Vuejs 2 - 从孩子去父母
问题描述:
当看路线,我有以下航线结构Vuejs 2 - 从孩子去父母
/event/:id/schedule/:id
我想那是什么,当用户进入一个子组件/路由,父隐藏的一些元素。当用户回到父母时,这些元素应该回来。
在父组件上,我试过看$ path对象,但没有任何东西被触发。在挂接/创建父项组件的方法上挂接钩子将不起作用,因为用户仍然处于这些路由中。我看到一些关于
watch(){
$route: {
console.log('route change');
}
}
我也试过这样:
route: {
canReuse: false
},
它没有工作,如果没有,我不希望使用它。
任何其他想法都会非常有帮助。谢谢。
答
刚刚在vue-router docs中发现了一些路由钩子/导航卫士。对父组件使用了以下方法。
beforeRouteUpdate (to, from, next) {
// called when the route that renders this component has changed,
// but this component is reused in the new route.
// For example, for a route with dynamic params /foo/:id, when we
// navigate between /foo/1 and /foo/2, the same Foo component instance
// will be reused, and this hook will be called when that happens.
// has access to `this` component instance.
},
这个工作完全