简体   繁体   English

深入观察不处理对象 Vue

[英]Deep watch in not working on object Vue

I have a watcher setup on an array and I have deep watch enabled on it, however the handler function does not trigger when the array changes, applications is defined in the object returned in data.我在一个数组上设置了一个观察程序,并且我启用了深度观察,但是当数组更改时处理函数不会触发,应用程序是在数据返回的对象中定义的。 Here's the code:这是代码:

  watch: {
    applications: {
      handler: function(val, oldVal) {
        console.log('app changed');
      },
      deep: true,
    },
    page(newPage) {
      console.log('Newpage', newPage);
    },
  },

Vue cannot detect some changes to an array such as when you directly set an item within the index: Vue 无法检测到数组的某些更改,例如当您直接在索引中设置项目时:

eg arr[indexOfItem] = newValue例如 arr[indexOfItem] = newValue

Here are some alternative ways to detect changes in an array:以下是一些检测数组变化的替代方法:

Vue.set(arr, indexOfItem, newValue)

or或者

arr.splice(indexOfItem, 1, newValue)

You can find better understanding of Array Change Detection here您可以 在此处找到对阵列更改检测的更好理解

If you reset your array with arr[ index ] = 'some value' , Vue doesn't track to this variable.如果你用arr[ index ] = 'some value'重置你的数组,Vue 不会跟踪这个变量。 It would better to use Vue array's mutation method .最好使用Vue数组的变异方法 These methods used to track array change detection by Vue.这些方法用于跟踪 Vue 的数组变化检测。

It is worked for me.它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM