vue.js - 从子组件Vue检测到父组件的条目
问题描述
我试图找到一种方法来检测我何时从子组件进入父组件。由于在访问子组件时已经加载了父组件,因此在返回父组件时我不能使用mounted() 和created() 生命周期钩子。
下面的代码是我的路线布局的示例。当我从“/foo/bar”导航回“/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
从你的父组件内部访问。
推荐阅读
- qt - 应用删除文本后,组合框的下拉菜单不会调整大小
- wordpress - 如果未选择 XX 国家并且结帐页面上的购物车包含 WooCommerce 中的特定产品,则禁用下订单按钮并显示消息
- loops - 以小于一个的步骤重复
- python - 使用 django-rest-framework 处理会话
- typescript - 以编程方式生成 d.ts(在内存中)
- python - 禁止在 pip-compile 的 requirements.txt 中添加 github 令牌
- javascript - JavaScript 正在工作,但不能同时工作
- python - 即使其中一张图像中的对象略微旋转,如何找到两张图像之间的差异
- google-cloud-platform - “请求被中止,因为没有可用的实例” - Google Cloud Functions(PubSub 触发器)
- mysql - 将 sql 代码转换为 django orm self 表并且不使用外键