Vue2.x全局路由钩子使用
router.js文件
import Vue from 'vue'
import VueRouter from 'vue-router'
import NotFound from '../404.vue'
import Hello from '../components/Hello.vue'
Vue.use(VueRouter)
let routes = [
{
path: '/404',
component: NotFound,
name: '404',
},
{
path: '/',
name: 'Hello',
component: Hello
} ,
{
path: '*',
redirect: { path: '/404' }
}
]
// 全局路由钩子
const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
console.log('路由钩子')
console.log(to)
console.log(from)
next()
})
export default router