vue 使用localstorage實(shí)現(xiàn)面包屑的操作
mutation.js代碼:
changeRoute(state, val) {
let routeList = state.routeList;
let isFind = false;
let findeIdex = 0;
//菜單欄和下拉的二級菜單
if (val['type'] == 'header' || val['type'] == 'secondHeader') {
routeList.length = 0;
//頂級菜單清除緩存
localStorage.removeItem("routeList");
}
let routes = routeList;
let localStorageList = localStorage.getItem('routeList');
if (localStorageList) {
//當(dāng)前頁刷新,使用緩存數(shù)據(jù)
routes = JSON.parse(localStorageList as any);
}
//遍歷是否有存入當(dāng)前路由位置
for (let i = 0; i < routes.length; i++) {
isFind = routes[i]['name'] == val['name'];
if (isFind) {
findeIdex = i;
break;
}
};
if (isFind) {
//有的話,清除當(dāng)前路由以后的數(shù)據(jù)
routes.splice(findeIdex + 1, routes.length - findeIdex - 1);
//修改緩存
localStorage.setItem('routeList', JSON.stringify(routes));
} else {
//存入全局變量
routes.push(val);
//設(shè)置緩存
localStorage.setItem('routeList', JSON.stringify(routes));
}
}
頁面使用:
//獲取面包屑緩存
let localStorageList1 = localStorage.getItem('routeList');
//ts寫法 as any
this.routeList = JSON.parse(localStorageList1 as any)
? JSON.parse(localStorageList1 as any)
: { name: 'test', url: '/test' };
知識點(diǎn):
1、localstorage
2、JSON.stringify()的作用是將 JavaScript 值轉(zhuǎn)換為 JSON 字符串,JSON.parse()將JSON字符串轉(zhuǎn)為一個對象
補(bǔ)充知識:vue+elementUI動態(tài)生成面包屑導(dǎo)航

項目需要動態(tài)生成面包屑導(dǎo)航,并且首頁可以點(diǎn)擊.其余為路徑顯示
<el-menu :unique-opened="true" router :default-active="$route.path" @select="handleSelect">
<div class="user-menu-box" v-for="menu in menus" :key="menu.id">
<el-menu-item v-if="!menu.child" :index="menu.path">
<icon :name="menu.icon" :w="20" :h="20"></icon>
<span slot="title" v-text="menu.name"></span>
</el-menu-item>
<el-submenu v-if="menu.child" :index="menu.path">
<template slot="title">
<icon :name="menu.icon" :w="20" :h="20"></icon>
<span slot="title" v-text="menu.name"></span>
</template>
<el-menu-item-group>
<el-menu-item v-for="subMenu in menu.child" :key="subMenu.id" v-text="subMenu.name"
:index="subMenu.path"></el-menu-item>
</el-menu-item-group>
</el-submenu>
</div>
</el-menu>
上面的代碼是elementUI組件的樣式,根據(jù)項目需要做了修改,搬運(yùn)的時候根據(jù)項目自己改改
export default {
data () {
return {
activeMenu: '',
indexBreadcrumbs: [],
menus: [{
id: '1',
name: '門戶管理',
icon: 'menhuguanli',
path: '#2',
child: [{
id: '1-1',
name: '輪播圖',
path: '/backstage/protaManage/turns'
}, {
id: '1-2',
name: '基礎(chǔ)數(shù)據(jù)',
path: '/backstage/protaManage/basis'
}, {
id: '1-3',
name: '分類管理',
path: '/backstage/protaManage/classify'
}, {
id: '1-4',
name: '內(nèi)容發(fā)布',
path: '/backstage/protaManage/content'
}]
}, {
id: '2',
name: '我的云盤',
icon: 'yunpan',
path: '/backstage/yunManage'
}, {
id: '3',
name: '管理菜單',
icon: 'shezhi',
path: '/backstage/menusManage'
}, {
id: '4',
name: '編輯板塊',
icon: 'fabuzhongxin',
path: '/backstage/editPlate'
}]
}
},
watch: {
$route () {
this.handChange()
}
},
computed: {
breadcrumbList () {
let breadcrumbs = []
let menuList = this.menus
this.indexBreadcrumbs.map(item => {
for (let i = 0; i < menuList.length; i++) {
if (item === menuList[i].path) {
breadcrumbs.push(menuList[i])
if (menuList[i].child) {
menuList = menuList[i].child
}
break;
}
}
})
return breadcrumbs
}
},
methods: {
handChange () {
this.$emit('hand-change', this.breadcrumbList)
},
handleSelect (index, indexPath) {
this.indexBreadcrumbs = indexPath
}
},
created () {
this.handChange()
}
}
上面的代碼是模擬的數(shù)據(jù),調(diào)用elememtUI的組件導(dǎo)航菜單的中的@select方法,有兩個參數(shù),可以自行打印

然后在computed中計算當(dāng)前選中的是哪一部分,取到返回值,然后$emit傳給父組件,
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/backstage' }">首頁</el-breadcrumb-item>
<el-breadcrumb-item v-for="o in breadcrumbList" :key="o.id">{{o.name}}
</el-breadcrumb-item>
</el-breadcrumb>
父組件中取到子組件傳過來的值后,直接渲染就行了
以上這篇vue 使用localstorage實(shí)現(xiàn)面包屑的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 引入AMap高德地圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue 引入AMap高德地圖的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Elementui?el-input輸入框校驗及表單校驗實(shí)例代碼
輸入框是使用非常多的元素,用來輸入用戶名、密碼等等信息,Element提供了功能和樣式豐富的輸入框,下面這篇文章主要給大家介紹了關(guān)于Elementui?el-input輸入框校驗及表單校驗的相關(guān)資料,需要的朋友可以參考下2023-06-06
vue3中unplugin-auto-import自動引入示例代碼
unplugin-auto-import 這個插件是為了解決在開發(fā)中的導(dǎo)入問題,下面這篇文章主要給大家介紹了關(guān)于vue3中unplugin-auto-import自動引入的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
解決antd 下拉框 input [defaultValue] 的值的問題
這篇文章主要介紹了解決antd 下拉框 input [defaultValue] 的值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
sublime如何配置開發(fā)VUE環(huán)境自動格式化代碼
這篇文章主要介紹了sublime如何配置開發(fā)VUE環(huán)境自動格式化代碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue開發(fā)中的base和publicPath的區(qū)別
本文主要介紹了vue開發(fā)中的base和publicPath的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

