从 News 进到 User
App.vue
<template>
<div>
<div>
<router-link to="/user?id=zs">User zs</router-link>
<router-link to="/user?id=ls">User ls</router-link>
<router-link to="/news">News</router-link>
</div>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App'
};
</script>
router/index.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import User from '@/components/User.vue';
import News from '@/components/News.vue';
Vue.use(VueRouter);
const router = new VueRouter({
routes: [
{
path: '/user',
component: User,
beforeEnter(to, from, next) {
console.log('#2 beforeEnter');
next();
}
},
{
path: '/news',
component: News
}
]
});
router.beforeEach((to, from, next) => {
console.log('#1 beforeEach');
next();
});
router.beforeResolve((to, from, next) => {
console.log('#4 beforeResolve');
next();
});
router.afterEach(() => {
console.log('#5 afterEach');
});
export default router;
components/User.vue
<template>
<div>User: {{ $route.query.id }}</div>
</template>
<script>
export default {
name: 'User',
beforeRouteLeave(to, from, next) {
console.log('beforeRouteLeave');
next();
},
beforeRouteUpdate(to, from, next) {
console.log('beforeRouteUpdate');
next();
},
beforeCreate() {
console.log('#6 beforeCreate');
},
created() {
console.log('#7 created');
},
beforeMount() {
console.log('#8 beforeMount');
},
mounted() {
console.log('#10 mounted');
},
beforeRouteEnter(to, from, next) {
console.log('#3 beforeRouteEnter');
next((cmp) => {
console.log('#9 beforeRouteEnter next', cmp);
});
}
};
</script>
|