Javascript 组件中嵌套道具同步对象的Vue.js更新值
您好,我对Vue.js v1.0.28有以下问题-我有一个组件Javascript 组件中嵌套道具同步对象的Vue.js更新值,javascript,components,vue.js,Javascript,Components,Vue.js,您好,我对Vue.js v1.0.28有以下问题-我有一个组件 Vue.component('question-editor', { template: require('./question-editor.html'), props: ['question'], methods: { addChoice() { this.question.choicesArr.push({ id: null,
Vue.component('question-editor', {
template: require('./question-editor.html'),
props: ['question'],
methods: {
addChoice() {
this.question.choicesArr.push({
id: null,
body:'zzz',
icon:'',
next:null,
});
console.log(this.question.choicesArr);
},
}
});
其中/question editor.html
:
...
<div class="box-body">
<div class="form-group" v-for="choice of question.choicesArr">
<input v-model="choice.body" type="text" class="form-control">
</div>
</div>
<div class="box-footer">
<button @pointerdown="addChoice" type="submit" class="btn btn-primary">
Add choice
</button>
</div>
。。。
增加选择
我在父组件中使用此组件的方式如下:
<question-editor :question.sync="currentQuestion"></question-editor>
问题是,当我按下“添加选项”按钮并运行方法
addChoice()
时,我在控制台中看到属性question.choicesArr
有新元素-但视图没有更改(我在屏幕上没有看到这个新元素-因此v-for没有“看到”这个新元素,也没有刷新自身)。如何在addChoice()
中刷新视图以在屏幕上查看问题中的新元素。ChoiceSAR
我猜vue 1.x不会像在中那样检测数组中的更改,您可以执行以下操作,让vue知道数组已在的帮助下更改
我找到了解决办法:
addChoice() {
this.question.choicesArr.push({
id: null,
body:'zzz',
icon:'',
next:null,
});
this.question = Object.assign({}, this.question, this.question);
console.log(this.question.choicesArr);
},
语句this.question=Object.assign({},this.question,this.question)
将\uuuuuu ob\uuuuu:Observer
和\uuu v-for\uuuuu 1:Fragment
设置为推送到数组中的新对象
我还尝试this.set(this.question,'question.choicesArr',this.question.choicesArr)代码>但这一组仅\uuuuu ob\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuoObserver
(非\uuuuuu v-for\uuuuuuuuuuu 1
)因此v-for不会更新
我在这里读到:我检查了它-由于数组中的新对象中缺少\uuuuu ob\uuuuuu:Observer
和\uu v-for\uu 1:Fragment
,此解决方案无法正常工作
addChoice() {
this.question.choicesArr.push({
id: null,
body:'zzz',
icon:'',
next:null,
});
this.question = Object.assign({}, this.question, this.question);
console.log(this.question.choicesArr);
},