【发布时间】:2019-03-21 08:11:52
【问题描述】:
为什么手表只触发一次?如下代码,当点击'sync'按钮时,id和text同时发生变化,但只触发了id() watch,需要再次点击'sync'按钮才能触发text() watch,为什么?
Vue.component('test-bar', {
props: ['id', 'text'],
template: `
<div>
<div>child: {{id}} {{text}}</div>
<button @click="on_click">sync</button>
</div>
`,
watch: {
id() {
this.$emit('update:id', this.id)
// this.$emit('update:text', this.text) // !!! notice here. only when i added this line will trigger text() watch
},
text() {
this.$emit('update:text', this.text)
}
},
methods: {
on_click() {
this.id = 1
this.text = 'child text'
}
}
})
new Vue({
el: "#app",
data: {
id: 0,
text: 'parent text',
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<test-bar :id.sync='id' :text.sync='text'></test-bar>
<div>parent: {{id}} {{text}}</div>
</div>
【问题讨论】:
标签: vue.js