vue路由劃分模塊并自動(dòng)引入方式
路由劃分模塊并自動(dòng)引入
創(chuàng)建路由文件
主路由文件index.js 內(nèi)容,這樣配置后只要在router目錄下按照xxx.router.js的格式創(chuàng)建路由文件,就可以自動(dòng)加入到路由容器中
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) let routerList = [] function importAllRouter(r) { r.keys().forEach( keys => { routerList.push(r(keys).default) }); } /** * * require.context 是webpack里面的方法 用于獲取文件 * 第一個(gè)參數(shù)是文件目錄 * 第二個(gè)參數(shù)是是否查看子目錄 * 第三個(gè)參數(shù)是什么類型的文件 * * */ importAllRouter( require.context( './', false,/\.router\.js/)) const routes = [ ...routerList , // 解構(gòu) 注意這一步 { path: '/', name: 'Home', component: () => import('views/Home/home') } ] const router = new VueRouter({ routes }) export default router
其他模塊路由文件,有一個(gè)主路由路徑剩余全為子路由
export default { { path: '/index', name: 'index', component: () => import('views/index/index'), children:[ ] } }
vue-router模塊劃分問(wèn)題
路由整體結(jié)構(gòu)
路由拆分指的是將路由的文件,按照模塊(modules)拆分,這樣方便路由的管理,更主要的是方便多人開發(fā)。具體要不要拆分,那就要視你的項(xiàng)目情況來(lái)定了,如果項(xiàng)目較小的話,也就一二十個(gè)路由,那么是拆分是非常沒必要的。但倘若你開發(fā)一些功能點(diǎn)較多的商城項(xiàng)目,路由可以會(huì)有一百甚至幾百個(gè),那么此時(shí)將路由文件進(jìn)行拆分是很有必要的。
src | -- router 路由目錄 | ---- common.js //通用路由:聲明通用路由 ---- index.js //導(dǎo)出相關(guān)配置 ---- permission.js //權(quán)限驗(yàn)證 ---- config | ---- _import_development.js //開發(fā)環(huán)境 ---- _import_production.js //生產(chǎn)環(huán)境 ---- modules //業(yè)務(wù)模塊 | ---- index.js //自動(dòng)化處理文件:自動(dòng)引入路由的核心文件 ---- home.js //路由1
1、區(qū)分線上和開發(fā)環(huán)境---config
開發(fā)環(huán)境使用懶加載會(huì)導(dǎo)致熱更新,導(dǎo)致更新變慢,所以開發(fā)環(huán)境使用全量默認(rèn)加載,生產(chǎn)環(huán)境使用懶加載
1)_import_development.js
/** ?* @return ?開發(fā)環(huán)境使用全量默認(rèn)加載 ?*/ module.exports = file => require('@/views/' + file + '.vue').default
2)_import_production.js
/** ?* @return ?生產(chǎn)環(huán)境使用懶加載 ?*/ module.exports = file => () => import('@/views/' + file + '.vue')
2、通用路由---common.js
const _import = require('./config/_import_' + process.env.NODE_ENV) export default [ ? // 默認(rèn)頁(yè)面 ? { ? ? path: '/', ? ? redirect: '/index', ? }, ? // 無(wú)權(quán)限頁(yè)面 ? { ? ? path: '/nopermission', ? ? name: '無(wú)權(quán)限頁(yè)面', ? ? component: _import('NoPermission') ? }, ? // 404 ? { ? ? path: '*', ? ? name: '404', ? ? component: _import('404') ? } ]
3、業(yè)務(wù)模塊---modules
以home.js為例
const _import = require('../config/_import_' + process.env.NODE_ENV) /** ?* 業(yè)務(wù)模塊home ?*/ export default [ ? // 首頁(yè) ? { ? ? path: '/index', ? ? name: '首頁(yè)', ? ? component: _import('home/index'), ? ? meta: { ? ? ? keepAlive: false, ? ? ? requiresAuth: true // 要求驗(yàn)證的頁(yè)面,在此情況下其子頁(yè)面也會(huì)被驗(yàn)證. ? ? }, ? } ]
業(yè)務(wù)模塊導(dǎo)出(關(guān)鍵部分):
modules下的index.js /** ?* 自動(dòng)化處理文件:自動(dòng)引入路由的核心文件 ?*/ const files = require.context('.', true, /\.js$/) let configRouters = [] /** ?* inject routers ?*/ files.keys().forEach(key => { ? if (key === './index.js') return ? configRouters = configRouters.concat(files(key).default) // 讀取出文件中的default模塊 }) export default configRouters?
4、導(dǎo)出所有---index.js
import Vue from 'vue' import Router from 'vue-router' import RouterModule from './modules' // 引入業(yè)務(wù)邏輯模塊 import RouterCommon from './common' // 引入通用模塊 import { routerMode } from '@/config/urls.js' Vue.use(Router) const router = new Router({ mode: routerMode, // history模式需要服務(wù)端支持 scrollBehavior(to, from, savedPosition) { if (savedPosition) { //如果savedPosition存在,滾動(dòng)條會(huì)自動(dòng)跳到記錄的值的地方 return savedPosition } else { return { x: 0, y: 0 } //savedPosition也是一個(gè)記錄x軸和y軸位置的對(duì)象 } }, routes: [ ...RouterCommon, ...RouterModule, ] }) export default router
5、權(quán)限驗(yàn)證---permission.js
import Vue from 'vue' import router from './index' import NProgress from 'nprogress' import 'nprogress/nprogress.css' NProgress.configure({ ? easing: 'ease', // 動(dòng)畫方式 ? ? ? speed: 500, // 遞增進(jìn)度條的速度 ? ? ? showSpinner: false, // 是否顯示加載ico ? ? ? trickleSpeed: 200, // 自動(dòng)遞增間隔 ? ? ? minimum: 0.3 // 初始化時(shí)的最小百分比 }) const cancelAxios = (next) => { ? let axiosPromiseArr = window.__axiosPromiseArr; ? if (!!axiosPromiseArr && axiosPromiseArr.length) { ? ? // console.log(axiosPromiseArr); ? ? let len = axiosPromiseArr.length; ? ? while (len--) { //從后向前終止請(qǐng)求,并刪除 cancelToken,避免數(shù)組索引帶來(lái)的問(wèn)題 ? ? ? axiosPromiseArr[len].cancel('cancel'); ? ? ? axiosPromiseArr.splice(len, 1); ? ? } ? ? //或者:window.__axiosPromiseArr = []; ? } ? next(); } router.beforeEach((to, from, next) => { ? // 每次切換頁(yè)面時(shí),調(diào)用進(jìn)度條 ? NProgress.start(); ? if (to.matched.some(record => record.meta.requiresAuth)) { // 哪些需要驗(yàn)證 ? ? if (!sessionStorage.getItem("token")) { // token存在條件 ?? ? ? ? next({ ? ? ? ? path: '/nopermission', // 驗(yàn)證失敗要跳轉(zhuǎn)的頁(yè)面 ? ? ? ? query: { ? ? ? ? ? redirect: to.fullPath // 要傳的參數(shù) ? ? ? ? } ? ? ? }) ? ? } else { ? ? ? cancelAxios(next); ? ? } ? } else { ? ? cancelAxios(next); ? } }) router.afterEach((to, from) => { ? // 在即將進(jìn)入新的頁(yè)面組件前,關(guān)閉掉進(jìn)度條 ? NProgress.done() ? document.title = to.name || '' }) export default router
使用方式:
main.js
import router from './router/permission' new Vue({ ? el: '#app', ? router, ? components: { App }, ? template: '<App/>' })
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)動(dòng)態(tài)列表點(diǎn)擊各行換色的方法
今天小編就為大家分享一篇vue實(shí)現(xiàn)動(dòng)態(tài)列表點(diǎn)擊各行換色的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue實(shí)現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實(shí)現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01使用Vue開發(fā)一個(gè)實(shí)時(shí)性時(shí)間轉(zhuǎn)換指令
我們就來(lái)實(shí)現(xiàn)這樣一個(gè)Vue自定義指令v-time,將表達(dá)式傳入的時(shí)間戳實(shí)時(shí)轉(zhuǎn)換為相對(duì)時(shí)間。下面小編給大家?guī)?lái)了使用Vue開發(fā)一個(gè)實(shí)時(shí)性時(shí)間轉(zhuǎn)換指令,需要的朋友參考下吧2018-01-01vue-music關(guān)于Player播放器組件詳解
這篇文章主要為大家詳細(xì)介紹了vue-music關(guān)于Player播放器組件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Vue.js 2.0 和 React、Augular等其他前端框架大比拼
這篇文章主要介紹了Vue.js 2.0 和 React、Augular等其他前端框架大比拼的相關(guān)資料,React 和 Vue 有許多相似之處,本文給大家提到,需要的朋友可以參考下2016-10-10vue子組件created方法不執(zhí)行問(wèn)題及解決
這篇文章主要介紹了vue子組件created方法不執(zhí)行問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10