Vue.js 如何从父级更新vuejs组件

Vue.js 如何从父级更新vuejs组件,vue.js,mutation,Vue.js,Mutation,我对VueJs有点迷茫,我尝试用组件和vue更新的数据制作一个vue 我了解如何使用$emit更新父值 如果可能的话,有人能打电话吗 Html代码 <div id="app2" v-cloak> <p>{{labels.lbl1}}: {{values.value}}</p> how direct set {{labels.lbl1}} here: <input type="text" v-model:value="values.value"&g

我对VueJs有点迷茫,我尝试用组件和vue更新的数据制作一个vue

我了解如何使用$emit更新父值

如果可能的话,有人能打电话吗

Html代码

<div id="app2" v-cloak>
  <p>{{labels.lbl1}}: {{values.value}}</p>
  how direct set {{labels.lbl1}} here: <input type="text" v-model:value="values.value"> can set the child value?
  <hr/>
  <child2 v-model:value="values.value" v-bind:lbl="labels.lbl1"></child2>
</div>

<template id="child2">
  <div>
     Set {{internallbl1}} in child:
     <input type="text" v-model="internalValue" >
     <p>Value : {{value}}</p>
  </div>
</template>
以下是JSFIDLE:

谢谢,, David

我的理解是,当父项中的值发生变化时,内部值不会发生变化。您可以在值上设置观察者,并在其更改时设置internalValue,如下所示:

Vue.component('child2', {
  template: '#child2',
  ...
  ...
  ...
  created: function() {
    // We initially sync the internalValue with the value passed in by the parent
    this.internalValue = this.value;
    this.internallbl1 = this.lbl;
  },
  watch: {
    'internalValue': function() {
      // When the internal value changes, we $emit an event. Because this event is 
      // named 'input', v-model will automatically update the parent value
      this.$emit('input', this.internalValue);
    },    
     value: function(newVal) {
        this.internalValue = newVal
     }
  },
});
见工作

Vue.component('child2', {
  template: '#child2',
  ...
  ...
  ...
  created: function() {
    // We initially sync the internalValue with the value passed in by the parent
    this.internalValue = this.value;
    this.internallbl1 = this.lbl;
  },
  watch: {
    'internalValue': function() {
      // When the internal value changes, we $emit an event. Because this event is 
      // named 'input', v-model will automatically update the parent value
      this.$emit('input', this.internalValue);
    },    
     value: function(newVal) {
        this.internalValue = newVal
     }
  },
});