目的
解放雙手,今後不用配置路由。當你看到項目中大批量的路由思考是拆分維護業務路由仍是統一入口維護時,無需多慮,router-auto是你的最優選擇,它幫你解決路由的維護成本,你只須要建立相應的文件夾,路由就能動態生成,路由攔截你能夠在main.js中去攔截他,總之比你想象的開發還要簡單。
實現效果
簡要用法
具體用法請實時查看github或者npm,下面作一些簡要用法介紹
引用
const RouterAuto = require('router-auto')
module.exports = {
entry: '...',
output: {},
module: {},
plugin:[
new RouterAuto()
]
}
複製代碼
項目結構
- package.json 等等文件與目錄
- src 項目目錄
- page 頁面目錄
- helloworld
- Index.vue 頁面入口
- hello.vue 業務組件
- router.js 額外配置
- demo
- test
- Index.vue 頁面入口
- test.vue 業務組件
- Index.vue 頁面入口
- home
上面的目錄結構生成的路由結構爲
import Vue from 'vue'
import Router from 'vue-router'
import helloworld from '@/page/helloworld/Index.vue'
import demo from '@/page/demo/Index.vue'
import demo_test from '@/page/demo/test/Index.vue'
import home from '@/page/home/Index.vue'
Vue.use(Router)
export default new Router({
mode:'history',
base:'/auto/',
routes:[{
path: '/helloworld/index',
name: 'helloworld',
component: helloworld
},{
path: '/demo/index',
name: 'demo',
component: demo
},{
path: '/demo/test/index',
name: 'demo_test',
component: demo_test
},{
path: '/home/index',
name: 'home',
component: home
}]
})
複製代碼
初始化參數配置new RouterAuto(options = {})
參數 |
說明 |
類型 |
默認值 |
必填項 |
contentBase |
根目錄即src平級目錄 |
String |
當前根目錄process.cwd() |
否 |
mode |
router中mode |
String |
history |
否 |
base |
router中base |
String |
/auto/ |
否 |
watcher |
是否啓用熱更新(請在dev環境啓用) |
Boolean |
false |
否 |
小缺陷
- 首先咱們的項目不須要子路由,因此都是平鋪路由,可是你能夠文件夾中建立文件夾在用文件夾規劃子路由,後續會升級幾個版本加入進去,固然看看使用了和需求,僞需求都砍掉
- 如今生成的.router.js文件在磁盤中,做者之後進一步優化放到內存中,一步一個腳印,共創大好河山
- 而後就沒缺陷了.... 但願提issue越多越好
本文參考與相關內容地址