老莫的笔记本  
  
查看: 1185|回复: 0

VUE 路由的模块化

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-12-2 22:58:48 | 显示全部楼层 |阅读模式
其实吧就是把vue-router 单独当做一个模块封装出来, 你还别说 ,我真的是 都快忘记 nodejs原生代码怎么写了---
好了,开始封装
src/router/router.js
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. Vue.use(VueRouter);
  4. // 3.1
  5. import Home from '../components/Home'
  6. import News from '../components/News'
  7. import Anli from '../components/Anli'
  8. import Info from '../components/Info'
  9. import One from '../components/One'
  10. import Two from '../components/Two'
  11. import Six from '../components/Six'
  12. import Three from '../components/Three'
  13. // 3.2
  14. const routes = [
  15.     { path: '/home', component: Home },
  16.     { path: '/news', component: News },
  17.     {
  18.       path: '/anli', component: Anli, children: [   //利用 children 来规定子路由 注意 子路由无/
  19.         { path: 'one', component: One }
  20.         , { path: 'two', component: Two }
  21.         , { path: 'six', name: 'lmsix', component: Six }  // 别名路由
  22.         , { path: 'three', redirect: '/news' , component: Three} //重定向 定到新闻页
  23.       ]
  24.     },
  25.     { path: '/info/:id/:name', component: Info }, //多传参
  26.     { path: '*', component: Home }, //默认首页
  27.   ]
  28. const router = new VueRouter({
  29.     routes // short for `routes: routes`
  30.   })
  31. export default router;
复制代码

写完之后 丢到 main.js 中
  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. // 单独配置路由模块后引入
  4. import router from './router/router.js'
  5. Vue.config.productionTip = false
  6. new Vue({
  7.   // 4
  8.   router,
  9.   render: h => h(App),
  10. }).$mount('#app')
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表