Vue学习之路之使用动态路由的注意事项

mac2024-04-10  32

提醒一下,当使用路由参数时,例如从 /user/foo 导航到 /user/bar,原来的组件实例会被复用。因为两个路由都渲染同个组件,比起销毁再创建,复用则显得更加高效。不过,这也意味着组件的生命周期钩子不会再被调用。

复用组件时,想对路由参数的变化作出响应的话,你可以简单地 watch (监测变化) $route 对象:

const User = { template: '...', watch: { '$route' (to, from) { // 对路由变化作出响应... } } }

或者使用 2.2 中引入的 beforeRouteUpdate 导航守卫:

const User = { template: '...', beforeRouteUpdate (to, from, next) { // react to route changes... // don't forget to call next() } }

 

最新回复(0)