原文地址:关于 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. python入门 -- 环境搭建(windows)

    1. 下载Anaconda Anaconda内置了python解释器及经常使用的库,提供了编译好的环境.根据自己的操作系统,自行从下面网站挑选一个较新的版本,下载安装即可. https://mirro ...

  2. MVCAPi Httpclient

    APi配制文件 删除修改api 显示和命名空间 新增

  3. 【转】priority_queue优先队列

    转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...

  4. QEMU KVM Libvirt手册(7): 硬件虚拟化

    在openstack中,如果我们启动一个虚拟机,我们会看到非常复杂的参数 qemu-system-x86_64 -enable-kvm -name instance-00000024 -S -mach ...

  5. Python爬虫实践 -- 记录我的第一只爬虫

    一.环境配置 1. 下载安装 python3 .(或者安装 Anaconda) 2. 安装requests和lxml 进入到 pip 目录,CMD --> C:\Python\Scripts,输 ...

  6. JavaScript中如何理解如何理解Array.apply(null, {length:5})

    先来看一个问题: 如何理解Array.apply(null, {length:5})的{length:5}? 我测试过Array.apply(null, {length:5}) //返回[undefi ...

  7. limit实现的分页查询

    背景:原先是一次性查询加载到前段,表格插件自动分页,最近查询的数据量越来越大,长的时候需要等好几十秒,决定自己写一个后端分页,我写的和网上大神的略有不同,不是后端写一个类封装分页的参数,每次查询都是穿 ...

  8. [Swift]LeetCode48. 旋转图像 | Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  10. [Swift]LeetCode508. 出现次数最多的子树元素和 | Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...