起因
上家公司的后臺管理系統都是刷表刷出來的,所用很久很久沒寫后臺管理系統了。換了工作后總算要開始搗騰router了,很久沒用都快忘光了,所以把一些通用的模塊記錄一下,也分享給需要的朋友們。
經過
//router.jslet routes = [ { path: '/', redirect: '/admin', }, { path: '/login', name: 'login', meta: {title: '登錄'}, component: () => import('./components/login.vue') }, { path: '/admin', name: 'admin', meta: {title: '主頁'}, component: () => import('./components/admin.vue'), children: [ { path: 'operation', name: 'operation', meta: {title: '運營管理'}, component: () => import('./components/admin/operation.vue') }, { path: 'order', name: 'order', meta: {title: '訂單中心'}, redirect: 'order/index', component: () => import('./components/admin/order.vue'), children: [ { path: 'index', name: 'index', meta: {title: ''}, component: () => import('./components/admin/ordercenter.vue') }, { path: 'detail', name: 'detail', meta: {title: '訂單詳情'}, component: () => import('./components/admin/orderdetail.vue') }, ] }, ] },]export default routes這個是我部分的router路徑配置表
/*面包屑路徑處理*/ eve_breadcrumbItem_change(){ var list = this.$route.fullPath.split('/')//list[0]:是空格 this.BreadcrumbItem = [] function fn(obj, arr, index,self) { if (obj.hasOwnProperty('children')&&obj['children'].length>0) { for (let one of obj.children) { if (one.name != 'index' && one.name == arr[index]) { self.BreadcrumbItem.push({'title': one.meta.title, 'path': list.slice(0,index+1).join('/')}) return one.hasOwnProperty('children')&&one['children'].length>0?fn(one,arr,index+1,self):false } } } } for(let one of this.$router.options.routes){ if(one.hasOwnProperty('name')&&one.name == list[1]){ this.BreadcrumbItem.push({'title': one.meta.title, 'path': one.path}) fn(one,list,2,this) } } }這個是就是本文的重點,其實也簡單,就是遞歸了下路徑名重新組裝了下數據給面包屑傳過去
watch: { '$route'(to, from) { this.eve_breadcrumbItem_change() }},...mounted() { this.eve_breadcrumbItem_change()},使用也簡單,無非watch檢測下路徑變化,避免刷新頁面時沒路徑,在mounted里再調用一下。
結果
結果嘛,自然就解決問題。不過路徑的配置可能會和大家的不同,我喜歡在分組下默認弄個index路徑,我覺得這樣結構比較好,這里大家注意下。
新聞熱點
疑難解答
圖片精選