1.this.$router
和 router
使用起來完全一樣,this.$router
不需要每個組件裏面導入路由
2.響應路由參數的變化
當使用路由參數時,例如從 /user/foo
導航到 /user/bar
,原來的組件實例會被複用。因爲兩個路由都渲染同個組件,比起銷燬再創建,複用則顯得更加高效。不過,這也意味着組件的生命週期鉤子不會再被調用。
解決方法,watch監聽路由變化
const User = {
template: '...',
watch: {
'$route' (to, from) {
// 對路由變化作出響應...
}
}
}
beforeRouteUpdate
導航守衛:
const User = {
template: '...',
beforeRouteUpdate (to, from, next) {
// react to route changes...
// don't forget to call next()
}
}