Vue-sync修饰符-组件中修改props内的值

ComponentA.vue






HelloWorld.vue








点击ComponentA 中的按钮:回调,title值修改成功了,但是会抛出vue的一个警告:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "title"

found in

--->  at src/components/ComponentA.vue
        at src/components/HelloWorld.vue
          at src/App.vue
           
经过研究发现一种修改方式:sync修饰符

修改ComponentA中的back方法:

 back () {
      console.log('back')
      // this.title = '回调了'
      this.$emit('titleChanged', '子向父组件传值')
      this.$emit('update:title', 'newTitle')
    }

修改HelloWorld中的布局:

    

你可能感兴趣的:(Vue-sync修饰符-组件中修改props内的值)