javascript - Vue: component failed to update $parent
问题描述
I want to update $parent data from child component. (I know it's bad but very conv for tightly bound component).
So I'm trying like is shown below, but it update field named 'undefined' instead:
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>
解决方案
You added this
keyword to the property of $parent.obj
. You don't need to use this
keyword in template.
Here is the working code: codesandbox
推荐阅读
- javascript - 滚动到顶部到原始滚动状态
- jquery - 调用所有类方法或函数的 JQuery 侦听器
- xamarin.android - 图像的长度和宽度是两倍,为什么?
- java - Java Flight Recorder - 没有活动对象
- graph - 点符号等级未将项目放置在同一水平线/平面上
- youtube-api - YouTube v3 API 搜索 etag 不一致
- android - 附近连接的最大连接设备数 - 说明
- php - Divi / Woocommerce 上带有 AJAX 和图像按钮的产品类别过滤器
- sql-server - 正确的大查询在 ASP Classic 中不起作用?
- azure - 通过 terraform 创建的 AKS 中 Windows 节点池的默认管理员密码是什么