vue双向绑定盲区

使用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.安装 可以简单地在页面引入Vue.js作为独立版本,Vue即被注册为全局变量,可以在页面使用了。 如果希望搭建...
    Awey阅读 11,284评论 4 129
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,799评论 0 3
  • 主要还是自己看的,所有内容来自官方文档。 介绍 Vue.js 是什么 Vue (读音 /vjuː/,类似于 vie...
    Leonzai阅读 3,537评论 0 25
  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,598评论 0 25
  • 第3章 基本概念 3.1 语法 3.2 关键字和保留字 3.3 变量 3.4 数据类型 5种简单数据类型:Unde...
    RickCole阅读 5,503评论 0 21

友情链接更多精彩内容