已知,子組件通過props獲取父組件傳過來的數據,而這個數據是無法在created、mounted生命周期中使用的,只能在beforeUpdated或者updated獲取到;
但是如果我們要使用這個數據再獲取其他數據並渲染到頁面,我們就不能在beforeUpdated或者updated操作,自相矛盾。
這就可以用到Vue的watch方法,先監聽子組件獲取到的數據,當數據有變化后(也就是數據獲取到后),再調用相應的方法。
如下,子組件獲取到數據detail,然后要在getTicket方法中使用這個數據,就要在watch中先監聽detail的變化,當detail有數據后,再調用getTicket方法,在方法中對數據進行使用
props: {
detail: ''
},
methods: {
async getTicket () {
console(this.detail)
}
},
watch: {
detail () {
this.getTicket()
}
}