gpt4 book ai didi

javascript - Vue : component failed to update $parent

转载 作者:行者123 更新时间:2023-11-28 14:29:37 25 4
gpt4 key购买 nike

我想从子组件更新 $parent 数据。 (我知道这很糟糕,但对于紧密绑定(bind)的组件来说非常转换)。

所以我尝试如下所示,但它会更新名为“未定义”的字段:

Vue.config.productionTip = false;

const child = {
template: `
<div class="hello">
<input type="text" v-model="$parent.obj[this.field]">
<pre>{{ $props }}</pre>
</div>`,

props: ['field'],
}

new Vue({
el: "#app",

components: {
child,
},

data() {
return {
obj: {
f1: 111,
f2: 222
}
}
},

});
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>

<div id="app">
parent:
<pre>{{ $data }}</pre>

<hr>

child:
<child field="f1" />
</div>

最佳答案

您已将 this 关键字添加到 $parent.obj 的属性中。您不需要在模板中使用 this 关键字。这是工作代码:codesandbox

关于javascript - Vue : component failed to update $parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51984494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com