vue+element實(shí)現(xiàn)頁面頂部tag思路詳解

這種tag如何寫?思路總結(jié)下:
1. 頁面渲染
1頁面顯示由數(shù)組循環(huán)得出,數(shù)組可存儲在store里
(1)存儲前判斷是否有重復(fù)的數(shù)據(jù),重復(fù)的話,先刪除再添加。
(2)沒有重復(fù)直接push
addTag: (state, tag) => {
const { fullPath, path, meta, query } = tag
if (tag.path === '/login') {
return false
}
const findIndex = state.tags.findIndex(item => item.path === tag.path)
console.log(findIndex)
if (findIndex >= 0) {
state.tags.splice(findIndex, 1, { fullPath, path, meta, query })
} else {
state.tags.push({ fullPath, path, meta, query })
}
},
2何時(shí)觸發(fā)這個(gè)添加路由方法,監(jiān)聽路由進(jìn)入的時(shí)候,調(diào)此方法將當(dāng)前this實(shí)例上的route對象攜帶過去。
computed: {
currentRoute() {
return this.$route
},
},
watch: {
$route: {
handler(val) {
if (val.name) {
this.addTags()
}
},
// 深度觀察監(jiān)聽
deep: true
}
},
methods:{
addTags() {
//this.$store.dispatch 先提交給action,由他異步處理處罰mutation里面的方法,改變state里面的tags值
this.$store.dispatch('user/addTag', this.currentRoute)
},}
此時(shí),tags數(shù)組里面已經(jīng)有值,由于默認(rèn)是白色,所以頁面上看不出,接下來就是給選中的標(biāo)簽高亮。
1element 有個(gè)參數(shù)可以設(shè)定,可以查文檔。
2選中的tag值是否等于當(dāng)前路由進(jìn)入的頁面一致,一致則為true。
<span v-for="(tag, index) in tags" :key="index" class="tag-span">
<el-tag
:closable="isCloseable"
:effect="setTagColor(tag)"
@close="closeTags(tag)"
@click="toTagRoute(tag)"
>
{{ tag.meta.title }}
</el-tag>
</span>
methods:{
setTagColor(tag) {
return this.currentRoute.path === tag.path ? 'dark' : 'plain'
},
}
此時(shí),tag的渲染和選中就完成了。
2. 來回切換tag
methods:{
toTagRoute(tag) {
this.$router.push({
path: tag.fullPath || tag.path
})
},
}
3. 刪除一個(gè)tag標(biāo)簽
1由于是數(shù)組,你無法確定用戶刪除哪一個(gè),所以需要遍歷找出用戶當(dāng)前選中的tag。然后刪除,同時(shí)更新store里的值。
2刪除當(dāng)前tag,高亮的標(biāo)簽是哪一個(gè)?這里是刪除標(biāo)簽的前一個(gè)標(biāo)簽,也就是數(shù)組最后一個(gè)元素。
methods:{
closeTags(tag) {
console.log(tag, 4444)
this.$store.dispatch('user/delTag', tag)
this.toLastTagRouter(this.$store.state.user.tags)//高亮刪除標(biāo)簽的前一個(gè)tag
},
toLastTagRouter(tags) {
//注意此處傳入tags是已刪除后的,所以不能使用splice==》改變原數(shù)組;slice==》不改變原數(shù)組拿去數(shù)組最后一個(gè)元素
const latestView = tags.slice(-1)[0]//tags數(shù)組最后一個(gè)元素
console.log(latestView)
if (latestView !== undefined && latestView.path !== undefined) {
const { fullPath, meta, path, query } = latestView
this.$router.push({ fullPath, meta, path, query })
}
},
}
//action
delTag({ commit }, tag) {
commit('delTag', tag)
},
//mutation
delTag: (state, tag) => {
//entries()對象變成一個(gè)可遍歷的數(shù)組【0,{name:a,age:'20'}】
//這里使用forEach和map也可以
for (const [i, v] of state.tags.entries()) {
if (v.path === tag.path) {
state.tags.splice(i, 1)
break
}
}
},
刪除全部標(biāo)簽
methods:{
closeAllTags() {
// 關(guān)閉所有 tag,僅剩余一個(gè)
this.$store.dispatch('user/delAllTags')
const { fullPath, meta, path, query } = this.$store.state.user.tags[0]
// 跳轉(zhuǎn)剩余 tag 路由
this.$router.push({ fullPath, meta, path, query })
},
}
//action
delAllTags({ commit }) {
commit('delAllTags')
},
//mutation
delAllTags: (state) => {
state.tags.splice(1, state.tags.length)
},
到此這篇關(guān)于vue+element如何實(shí)現(xiàn)頁面頂部tag的文章就介紹到這了,更多相關(guān)vue element頁面頂部tag內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式
這篇文章主要介紹了vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn)
本文主要介紹了Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue監(jiān)聽頁面滾動到某個(gè)高度觸發(fā)事件流程
這篇文章主要介紹了vue監(jiān)聽頁面滾動到某個(gè)高度觸發(fā)事件流程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
使用vue-cli創(chuàng)建vue2項(xiàng)目的實(shí)戰(zhàn)步驟詳解
相信大部分Vue開發(fā)者都使用過vue-cli來構(gòu)建項(xiàng)目,它的確很方便,但對于很多初級開發(fā)者來說,還是要踩不少坑的,下面這篇文章主要給大家介紹了關(guān)于使用vue-cli創(chuàng)建vue2項(xiàng)目的實(shí)戰(zhàn)步驟,需要的朋友可以參考下2023-01-01
Vue路由傳遞參數(shù)與重定向的使用方法總結(jié)
路由的本質(zhì)就是一種對應(yīng)關(guān)系,比如說我們在url地址中輸入我們要訪問的url地址之后,瀏覽器要去請求這個(gè)url地址對應(yīng)的資源,下面這篇文章主要給大家介紹了關(guān)于Vue路由傳遞參數(shù)與重定向的使用方法,需要的朋友可以參考下2022-10-10

