Vuejs:使用keep alive的子routerview组件的生命周期钩子

Vuejs: Lifecycle hooks of child routerview components using keep alive

为了清楚理解,我将以更笼统的方式解释我的问题,这里是 jsFiddle

我有两条主要路线,它们在 router-view

中显示了两个不同的组件

Route-2 内部包含另一个 router-view,其中显示两条子路线:

当单击 Route-2 时,它会打开带有 sub-route-a 的 Route-2 的组件 在其 router-view

中预先打开

Route-2 中的主路由视图和路由视图都包含在 keep-alive 标签内,以便缓存

缓存和一切都按预期正常工作。

我添加了所有生命周期钩子并使用 console.log 查看调用了哪个钩子

我的问题来了

将您的 vue 版本升级到 2.2.0 或更高版本。 fiddle 有 2.3.2,最新的。而你本地的版本是2.1.0.

In 2.2.0 and above, activated and deactivated will fire for all nested components inside a tree.

阅读此处:https://vuejs.org/v2/api/#keep-alive

更新使用:npm update --save vue