美文网首页
vue双向绑定盲区

vue双向绑定盲区

作者: 广告位招租 | 来源:发表于2019-02-21 16:00 被阅读0次

使用vue双向绑定的时候,有时候会遇到没有检测到数据变化的情况,以下情况,是需要在平常工作和使用中注意的问题

数组盲区

vue包含一组观察数组变异的方法,使用这些方法也会触发视图更新

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()

但是受到某些限制,vue无法检测到以下数组变动(视图不会更新)

<template>
    <div>
        <button @click="event">click</button>
        {{items}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                items: ['a', 'b', 'c']
            }
        },
        methods: {
            event() {
                this.items[1] = 'x' // 不是响应式的
                this.items.length = 2 // 不是响应式的
                console.log(this.items)
            }
        }
    }
</script>
image.png

为了解决这个问题,需要使用$set

<template>
    <div>
        <button @click="event">click</button>
        {{items}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                items: ['a', 'b', 'c']
            }
        },
        methods: {
            event() {
                // this.items[1] = 'x' 
                // this.items.length = 2 
                this.$set(this.items, 1, 'x')
                this.items.splice(2)
                console.log(this.items)
            }
        }
    }
</script>
image.png

对象盲区

vue不能检测对象属性的添加和删除

<template>
    <div>
        <button @click="event">click</button>
        {{items}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                items: {
                    a: 1 // 是响应式的
                }
            }
        },
        methods: {
            event() {
                this.items.b = 2 // 不是响应式的
                console.log(this.items)
            }
        }
    }
</script>
image.png

这里有两种解决方法

  1. 在data声明时,就给出b: ''的声明
<template>
    <div>
        <button @click="event">click</button>
        {{items}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                items: {
                    a: 1 // 是响应式的
                }
            }
        },
        methods: {
            event() {
                this.$set(this.items, 'b', 2) // 是响应式的
                console.log(this.items)
            }
        }
    }
</script>
image.png

当你想添加多个新属性到某个对象上时,也可以利用Object.assign(),但是不要直接给this.yourObject添加,例如

// 此时也不是响应的
Object.assign(vm.items, {
  c: 3
})

可以直接赋值给原有对象

// 此时是响应式的
this.items = Object.assign({}, this.items, {
  v: 3
})

为什么不能实现响应式?

https://segmentfault.com/a/1190000015783546

相关文章

  • vue双向绑定盲区

    使用vue双向绑定的时候,有时候会遇到没有检测到数据变化的情况,以下情况,是需要在平常工作和使用中注意的问题 数组...

  • 深入Vue响应式原理

    1.Vue的双向数据绑定 参考 vue的双向绑定原理及实现Vue双向绑定的实现原理Object.definepro...

  • Vue 中的双向数据绑定

    双向绑定 单向数据流 双向绑定 or 单向数据流 Vue 是单向数据流,不是双向绑定 Vue 的双向绑定是语法糖 ...

  • vue 之 input 的value绑定

    vue双向绑定值 vue单向绑定值

  • Vue双向绑定

    Vue双向绑定 Obejct.defineProperty的setter/getter和发布订阅 Vue双向绑定原...

  • 2020-08-27 前端面试题(vue)

    一、 vue双向绑定得原理: 二、vue2.0与vue3.0双向绑定得区别:

  • VUE(面试题)

    1、vue双向绑定原理vue2.x:vue双向绑定的原理是利用了Object.defineProperty()这个...

  • 02Vue.js快速入门-Vue入门之数据绑定

    2.1. 什么是双向绑定? Vue框架很核心的功能就是双向的数据绑定。 双向是指:HTML标签数据 绑定到 Vue...

  • 02Vue.js的数据绑定

    理解Vue的双向数据绑定 Vue有一个显著的地方就是它拥有双向数据绑定功能,那么何为双向数据绑定呢?双向是指:HT...

  • 2022前端面试准备(一)(vue篇)

    vue双向绑定 vue2.0 vue双向绑定主要是在observer(数据监听器)中通过Object.define...

网友评论

      本文标题:vue双向绑定盲区

      本文链接:https://www.haomeiwen.com/subject/hdwryqtx.html