鍍金池/ 問答/HTML/ vue中父組件通過props傳值同步問題

vue中父組件通過props傳值同步問題

直接上代碼:

    // html
    <div id="app">
      <p @click="clickChange">click me</p>
      <my-child :test="test"></my-child>
    </div>
    
    // js
    Vue.component('my-child', {
        props: [
          'test'
      ],
      data() {
          return {
            myTest: this.test
        }
      },
      template: '<div>{{ myTest }}</div>'
    })
    new Vue({
      el: "#app",
      data: {
        test: 'test'
      },
      methods: {
          'clickChange' () {
            this.test = 'haha'
          console.log(this.test)
        }
      }
    })

當(dāng)父組件傳過來的test改變時(shí),但子組件賦值到的myTest卻沒有改變,請(qǐng)問有什么方法可以快捷得到嗎?除了watch方法以外

回答
編輯回答
你的瞳

可以在子組件template中直接綁定test或者使用computed return test; 然后將計(jì)算屬性綁定到模板中

2018年4月24日 19:40
編輯回答
你好胸
computed: {
    myTest() {
        return this.test
    }
}
2017年3月10日 20:09