Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 组件中嵌套道具同步对象的Vue.js更新值_Javascript_Components_Vue.js - Fatal编程技术网

Javascript 组件中嵌套道具同步对象的Vue.js更新值

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.js v1.0.28有以下问题-我有一个组件

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);

},