|
其实吧就是把vue-router 单独当做一个模块封装出来, 你还别说 ,我真的是 都快忘记 nodejs原生代码怎么写了---
好了,开始封装
src/router/router.js- import Vue from 'vue'
- import VueRouter from 'vue-router'
- Vue.use(VueRouter);
- // 3.1
- import Home from '../components/Home'
- import News from '../components/News'
- import Anli from '../components/Anli'
- import Info from '../components/Info'
- import One from '../components/One'
- import Two from '../components/Two'
- import Six from '../components/Six'
- import Three from '../components/Three'
- // 3.2
- const routes = [
- { path: '/home', component: Home },
- { path: '/news', component: News },
- {
- path: '/anli', component: Anli, children: [ //利用 children 来规定子路由 注意 子路由无/
- { path: 'one', component: One }
- , { path: 'two', component: Two }
- , { path: 'six', name: 'lmsix', component: Six } // 别名路由
- , { path: 'three', redirect: '/news' , component: Three} //重定向 定到新闻页
- ]
- },
- { path: '/info/:id/:name', component: Info }, //多传参
- { path: '*', component: Home }, //默认首页
- ]
- const router = new VueRouter({
- routes // short for `routes: routes`
- })
- export default router;
复制代码
写完之后 丢到 main.js 中- import Vue from 'vue'
- import App from './App.vue'
- // 单独配置路由模块后引入
- import router from './router/router.js'
- Vue.config.productionTip = false
- new Vue({
- // 4
- router,
- render: h => h(App),
- }).$mount('#app')
复制代码
|
|