javascript - Vue : component failed to update $parent

标签 javascript vue.js vuejs2 vue-component

我想从子组件更新 $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/

相关文章:

javascript - 在固定位置滚动

javascript - jQuery 动画滚动到 anchor 不工作?

typescript - Vuex Typescript 我收到错误 "Member ' someMutation' implicitly has an 'any' type."in the Component.ts file

vue.js - 错误类型错误 : The 'compilation' argument must be an instance of Compilation

javascript - 用卡片实现 Vue 可拖动?

javascript - 403(CORS 未启用或未找到此请求的匹配规则。)[VueJs]

javascript - 序列化 - 类型错误 : Cannot read property 'define' of undefined

javascript - Snapsvg 变换线

vue.js - Vue 在路由器上进入过渡

google-maps - 谷歌地图 : Cannot read property 'setDirections' of undefined