The problem of the parent component passing the value of Synchronize through props in vue

go straight to the code:

</my-child>
    </div>
    
    // js
    Vue.component("my-child", {
        props: [
          "test"
      ],
      data() {
          return {
            myTest: this.test
        }
      },
      template: "<div>{{ myTest }}</div>"
    })
    new Vue({
      el: "-sharpapp",
      data: {
        test: "test"
      },
      methods: {
          "clickChange" () {
            this.test = "haha"
          console.log(this.test)
        }
      }
    })

when the test passed by the parent component changes, but the myTest assigned to the child component does not change, is there any way to get it quickly? Except for the watch method

Apr.01,2021

you can bind test directly in the subcomponent template or use computed return test; and then bind the calculation property to the template


computed: {
    myTest() {
        return this.test
    }
}
Menu