1
在下面的示例中,我使用created
生命週期來訂閱事件服務。這是正常的做法嗎?有更合適的方法或生命週期方法來做這種東西嗎?哪個生命週期鉤子用於初始化?
const ViewComponent = {
data(){
return {
pathname: window.location.pathname
}
},
created(){
eventService.on('routeResolved', (route) => {
this.pathname = route.pathname
})
},
computed: {
component() {
return routes[this.pathname]
}
},
render (h) {
return h(this.component)
}
}
'mounted'是另一種選擇,如果您的服務期望組件的DOM在那裏,則更好。 – ceejayoz