我试图找到一种方法来检测我何时从子组件进入父组件。由于在访问子组件时,父组件已经被加载,所以当我回到父组件时,我不能使用挂载()或创建()生命周期钩子。
下面的代码是我的路由如何布局的一个例子。当我从'foobar'导航回'foo'时,如何在Foo组件中检测到?
const routes = [
{ path: '/foo', component: Foo,
children: [
{ path: 'bar', component: Bar }
]}
]
你可以使用 导航仪 来获取之前的路径并进行检查。
在你的父组件中,你可以添加这个。
beforeRouteEnter (to, from, next) {
// here you can save to and check it in mounted for instance
// call next and pass a callback to access the component when it is ready
next(parentComp => {
parentComp.navigatedFrom = from
})
},
然后你就可以访问 navigatedFrom
从你的父组件内部。