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

這種tag如何寫?思路總結(jié)下:
1. 頁(yè)面渲染
1頁(yè)面顯示由數(shù)組循環(huán)得出,數(shù)組可存儲(chǔ)在store里
(1)存儲(chǔ)前判斷是否有重復(fù)的數(shù)據(jù),重復(fù)的話,先刪除再添加。
(2)沒(méi)有重復(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)聽(tīng)路由進(jìn)入的時(shí)候,調(diào)此方法將當(dāng)前this實(shí)例上的route對(duì)象攜帶過(guò)去。
computed: {
currentRoute() {
return this.$route
},
},
watch: {
$route: {
handler(val) {
if (val.name) {
this.addTags()
}
},
// 深度觀察監(jiān)聽(tīng)
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)是白色,所以頁(yè)面上看不出,接下來(lái)就是給選中的標(biāo)簽高亮。
1element 有個(gè)參數(shù)可以設(shè)定,可以查文檔。
2選中的tag值是否等于當(dāng)前路由進(jìn)入的頁(yè)面一致,一致則為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. 來(lái)回切換tag
methods:{
toTagRoute(tag) {
this.$router.push({
path: tag.fullPath || tag.path
})
},
}
3. 刪除一個(gè)tag標(biāo)簽
1由于是數(shù)組,你無(wú)法確定用戶刪除哪一個(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==》改變?cè)瓟?shù)組;slice==》不改變?cè)瓟?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()對(duì)象變成一個(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)頁(yè)面頂部tag的文章就介紹到這了,更多相關(guān)vue element頁(yè)面頂部tag內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element-ui前端使用print-js實(shí)現(xiàn)打印功能(可自定義樣式)
Print.js主要是為了幫助我們直接在瀏覽器中開發(fā)打印功能,下面這篇文章主要給大家介紹了關(guān)于vue+element-ui前端使用print-js實(shí)現(xiàn)打印功能(可自定義樣式)的相關(guān)資料,需要的朋友可以參考下2022-11-11
Vue項(xiàng)目中在父組件中直接調(diào)用子組件的方法
這篇文章主要給大家介紹了Vue項(xiàng)目中如何在父組件中直接調(diào)用子組件的方法,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11
VUE UPLOAD 通過(guò)ACTION返回上傳結(jié)果操作
這篇文章主要介紹了VUE UPLOAD 通過(guò)ACTION返回上傳結(jié)果操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vant的picker組件設(shè)置文字超長(zhǎng)滾動(dòng)方式
這篇文章主要介紹了vant的picker組件設(shè)置文字超長(zhǎng)滾動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解
這篇文章主要介紹了vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

