import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter) //挂载到vue对象中
import router from '@/routers/' //路由配置对象
new Vue({
router, //挂载到根实例对象app上,这里必须叫router,也可以router:其他名字
render: (h) => h(App),
}).$mount('#app')
--common.js 通用路由配置文件
--people.js 具体项目路由文件
--index.js 引入其他路由文件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
let routes = []
const requireContext = require.context('./', true, /\.js$/) //加载其他路由文件
requireContext.keys().forEach((filename) => {
if (filename === './index.js') {
return
}
const routeModule = requireContext(filename) // 获取文件内容
routes = [...routes, ...routeModule.default] // 合并两个数组内容
})
const router = new VueRouter({
routes,
}) //生成router实例
//最后添加404守卫路由
router.addRoutes([
{
path: '*',
name: '404',
component: () => import('@/views/404.vue'),
},
])
//配置全局路由守卫
router.beforeEach((to, from, next) => {
Nprogress.start()
document.title = from.meta.title
next()
})
router.afterEach((to, from) => {
console.log(from.name)
document.title = to.meta.title
Nprogress.done()
})
export default router
export default [
{
path: '/404',
name: 'pge404',
component: () => {
return import('../views/404.vue') //引入404组件
},
meta: { title: '404' },
},
]