简体   繁体   English

VueJS V-bind:sync="{object}" -- 在父节点中观察子节点发出的数据

[英]VueJS V-bind:sync="{object}" -- watching data emitted from child in parent

I am writing an app in which I have a parent component that passes an Object, this.form to a child form component via v-bind:sync="form" , with the child form emitting values for each of the keys of the parent on @input.我正在编写一个应用程序,其中我有一个父组件,它通过v-bind:sync="form"将对象this.form传递给子表单组件,子表单为父级的每个键发出值在@输入上。

I want to watch the value of this.form in the parent, but it seems the object on the parent is not reactive in the case of v-bind:sync, even though on a submit action in the parent I submit this.form with all of the emitted values from the child.我想在父级中this.form的值,但在 v-bind:sync 的情况下,父级上的对象似乎没有反应,即使在父级中的提交操作上我提交this.form从孩子发出的所有值。

Code example Parent:代码示例父:

<ParentComponent>

  <ChildComponent v-bind:sync="form"/>

  {{form}} <--- this doesn't update after child component emits

   <button @click="submit"> submit actions here have access to updated this.form... </button>
</ParentComponent>

...

data() {
  return {
      form: { ...some object keys}
}

You can use deep in watcher to watch object properties change.您可以使用deep in watcher 来观察对象属性的变化。

Something like就像是

  watch: {
    form: {
      handler() {
        console.log('form change')
      },
      deep: true
    }
  },

As in document文档

To also detect nested value changes inside Objects, you need to pass in deep: true in the options argument.要同时检测对象内部的嵌套值更改,您需要在选项参数中传递 deep: true 。 Note that you don't need to do so to listen for Array mutations.请注意,您不需要这样做来侦听 Array 突变。

I created a demo here我在这里创建了一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM