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