原文地址:关于 vue 不能 watch 数组变化 和 对象变化的解决方案

vue 监听数组和对象的变化

vue 监听数组

vue 实际上可以监听数组变化,比如:

data () {
return {
watchArr: [],
};
},
watchArr (newVal) {
console.log('监听:' + newVal);
},
created () {
setTimeout(() => {
this.watchArr = [1, 2, 3];
}, 1000);
},

再如使用 splice(0, 2, 3) 从数组下标 0 删除两个元素,并在下标 0 插入一个元素 3:

data () {
return {
watchArr: [1, 2, 3],
};
},
watchArr (newVal) {
console.log('监听:' + newVal);
},
created () {
setTimeout(() => {
this.watchArr.splice(0, 2, 3);
}, 1000);
},

push 数组也能够监听到

vue 无法监听数组变化的情况

但是,数组在下面两种情况无法监听:

  1. 利用索引直接设置一个数组项时,例如:arr[indexOfItem] = newValue;
  2. 修改数组的长度时,例如:arr.length = newLength;

举例无法监听数组变化的情况

  1. 利用索引直接修改数组值

    data () {
    return {
    watchArr: [{
    name: 'krry',
    }],
    };
    },
    watchArr (newVal) {
    console.log('监听:' + newVal);
    },
    created () {
    setTimeout(() => {
    this.watchArr[0].name = 'xiaoyue';
    }, 1000);
    },
  1. 修改数组的长度
    长度大于原数组就将后续元素设置为 undefined
    长度小于原数组就将多余元素截掉

    data () {
    return {
    watchArr: [{
    name: 'krry',
    }],
    };
    },
    watchArr (newVal) {
    console.log('监听:' + newVal);
    },
    created () {
    setTimeout(() => {
    this.watchArr.length = 5;
    }, 1000);
    },

    上面的 watchArr 变成
    

vue 无法监听数组变化的解决方案

  1. this.$set(arr, index, newVal);

    data () {
    return {
    watchArr: [{
    name: 'krry',
    }],
    };
    },
    watchArr (newVal) {
    console.log('监听:' + newVal);
    },
    created () {
    setTimeout(() => {
    this.$set(this.watchArr, 0, {name: 'xiaoyue'});
    }, 1000);
    },
  1. 使用数组 splice 方法可以监听,例子上面有

  2. 使用临时变量直接赋值的方式,原理与直接赋值数组一样

    data () {
    return {
    watchArr: [{
    name: 'krry',
    }],
    };
    },
    watchArr (newVal) {
    console.log('监听:' + newVal);
    },
    created () {
    setTimeout(() => {
    let temp = [...this.watchArr];
    temp[0] = {
    name: 'xiaoyue',
    };
    this.watchArr = temp;
    }, 1000);
    },

vue 监听对象

vue 可以监听直接赋值的对象

this.watchObj = {name: 'popo'};

vue 不能监听对象属性的添加、修改、删除

vue 监听对象的解决方法

  1. 使用 this.$set(object, key, value)
  2. 使用深度监听 deep: true,只能监听原有属性的变化,不能监听增加的属性
    mounted () {
    // 这里使用深度监听 blog 对象的属性变化,会触发 getCatalog 方法
    this.$watch('blog', this.getCatalog, {
    deep: true,
    });
    },
  1. 使用 this.set(obj, key, val) 来新增属性(vue 无法监听 this.set 修改原有属性)

    this.$set(this.watchObj, 'age', 124);

delete this.watchObj[‘name’](vue 无法监听 delete 关键字来删除对象属性)

  1. 使用 Object.assign 方法,直接赋值的原理监听(最推荐的方法)

    this.watchObj = Object.assign({}, this.watchObj, {
    name: 'xiaoyue',
    age: 15,
    });

原文地址:

关于 vue 不能 watch 数组变化 和 对象变化的解决方案

关于 vue 不能 watch 数组变化 和 对象变化的解决方案的更多相关文章

  1. vue 不能检测数组长度 值变化原因解析

    1.vue不能检测数组长度或者值的变化 (1)数组长度变化 未检测到 <!DOCTYPE html> <html lang="en"> <head&g ...

  2. vue中改变数组的值视图无变化

    今天开发的时候遇到一个多选取消点击状态的,渲染的时候先默认都选中,然后可以取消选中,自建了一个全为true的数组,点击时对应下标的arr[index]改为false,数据改变了状态没更新,突然想起来单 ...

  3. 在vue中使用watch监听对象或数组

    最近发现在vue中使用watch监听对象或者数组时,当数组或者对象只是单一的值改变时,并不会出发watch中的事件. 在找问题过程中,发现当数组使用push一类的方法时,会触发watch,如果只是单一 ...

  4. vue数组更新界面无变化

    1. vue数组更新界面无变化 1.1. 说明 对数组进行更新或者添加,一定要注意方式,我的情况是数组套数组,双重循环,在造数据的时候,不断从尾部添加数据,所以写成了如下形式,每次下拉都会去加载一批相 ...

  5. vue 绑定class、v-bind:style(对象语法、数组语法)

    绑定 HTML Class 我们可以传给 v-bind:class 一个对象,以动态地切换 class: 内联样式在模板里 <div id="div1" :class=&qu ...

  6. vue : watch、computed、以及对象数组

    watch和computed是vue框架中很重要的特性. 那么,他们是怎么作用于对象数组的? 今天我们就来探究一下. 上代码. <template> <div class=" ...

  7. vue 快速入门 系列 —— 侦测数据的变化 - [基本实现]

    其他章节请看: vue 快速入门 系列 侦测数据的变化 - [基本实现] 在 初步认识 vue 这篇文章的 hello-world 示例中,我们通过修改数据(app.seen = false),页面中 ...

  8. vue 快速入门 系列 —— 侦测数据的变化 - [vue 源码分析]

    其他章节请看: vue 快速入门 系列 侦测数据的变化 - [vue 源码分析] 本文将 vue 中与数据侦测相关的源码摘了出来,配合上文(侦测数据的变化 - [基本实现]) 一起来分析一下 vue ...

  9. vue 快速入门 系列 —— 侦测数据的变化 - [vue api 原理]

    其他章节请看: vue 快速入门 系列 侦测数据的变化 - [vue api 原理] 前面(侦测数据的变化 - [基本实现])我们已经介绍了新增属性无法被侦测到,以及通过 delete 删除数据也不会 ...

随机推荐

  1. vue 图片预览插件

    https://github.com/daidaitu1314/vue2-preview //cnpm cnpm install vue2-preview -save //引入 import VueP ...

  2. 关于阿里云Centos7 Mailx发送邮件失败的处理

    开始通过配置系统自带的mail 发送邮件 在其中一台服务器怎么都不行 /etc/mail.rc配置: set from="邮箱名" set smtp="smtp.163. ...

  3. js深度复制三种方法

    1.用递归的方式进行深度复制 2.用JSON.stringify加上JSON.parse()进行深度复制 3.用jquery中自带的方法$.extend()进行深度复制 具体实现代码可百度自行查询

  4. Dubbo+zookeeper构建高可用分布式集群(二)-集群部署

    在Dubbo+zookeeper构建高可用分布式集群(一)-单机部署中我们讲了如何单机部署.但没有将如何配置微服务.下面分别介绍单机与集群微服务如何配置注册中心. Zookeeper单机配置:方式一. ...

  5. 一个月薪两万的Web安全工程师要掌握哪些技能?

    作为一个薪水两万起步的工作,我想知道这些牛人们都会哪些技能呢? Web安全相关概念.熟悉渗透相关工具.渗透实战操作.关注安全圈动态.熟悉Windows/Kali Linux.服务器安全配置.脚本编程学 ...

  6. Java编程题: 写一个Singleton出来

    Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 一般Singleton模式通常有几种种形式: 第一种形式:  定义一个类,它的构造函数为private的, ...

  7. [Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [Swift]LeetCode434. 字符串中的单词数 | Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  9. [Swift]LeetCode539. 最小时间差 | Minimum Time Difference

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  10. [Swift]LeetCode637. 二叉树的层平均值 | Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...