我有一个数组:
basicForm.schema = [ {},{} // I want to watch only this ]
我试过这样做:
‘basicForm.schema[1].value’: { handler (schema) { const plan = schema.find(field => { return field.name === ‘plan’ }) },deep: true },
但是我收到了这个错误:
vue.js?3de6:573 [Vue warn]: Failed watching path:
“basicForm.schema[1]” Watcher only accepts simple dot-delimited paths.
For full control,use a function instead.
这样做的正确方法是什么?
解决方法
您可以改为观察计算属性:
new Vue({ el: '#app',data: { basicForm: { schema: [ {a: 1},{b: 2} // I want to watch only this ] } },computed: { bToWatch: function() { return this.basicForm.schema[1].b } },methods: { incB: function() { this.basicForm.schema[1].b++ } },watch: { bToWatch: function(newVal,oldVal) { console.log(newVal) } } });
<script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> <button @click="incB()">Inc</button> </div>