vue-element-template增加多標(biāo)簽頁tagsview

image.png

//注釋后面 【這個(gè)新增的】 表示我加的
如圖就是要實(shí)現(xiàn)這種效果參考鏈接:https://www.cnblogs.com/flypig666/p/11854538.html

一荣德、從vue-element-admin復(fù)制文件:
  vue-admin-template\src\layout\components\TagsView 文件夾
  vue-admin-template\src\store\modules\tagsView.js

二望伦、修改 vue-admin-template\src\layout\components\AppMain.vue :AppMain.vue文件芥牌,修改如下:

<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <!-- <router-view :key="key" />-->
      <keep-alive :include="cachedViews">  <!--這個(gè)是新增的-->
        <router-view></router-view>
      </keep-alive>
    </transition>
  </section>
</template>

<script>
export default {
  name: 'AppMain',
  computed: {
    key () {
      return this.$route.path
    },
    cachedViews () {   <!--這個(gè)是新增的-->
      return this.$store.state.tagsView.cachedViews
    }
  }
}
</script>
<style lang="scss" scoped>
.app-main {
  /*50 = navbar  */
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
  overflow: hidden;
}
.fixed-header + .app-main {
  padding-top: 50px;
}

.hasTagsView {
  .app-main {
    /* 84 = navbar + tags-view = 50 + 34 */
    min-height: calc(100vh - 84px);
  }

  .fixed-header + .app-main {
    padding-top: 84px;
  }
}
</style>


三、修改vue-admin-template\src\layout\components\index.js鸳惯,新增如下行:

export { default as Navbar } from './Navbar'
export { default as Sidebar } from './Sidebar'
export { default as AppMain } from './AppMain'
export { default as TagsView } from './TagsView'  //這個(gè)是新增的

四鸦做、在 vue-admin-template\src\layout\index.vue 文件中 新增 tagsview標(biāo)簽


image.png

五折欠、修改 vue-admin-template\src\store\getters.js贝或,增加:

const getters = {
  sidebar: state => state.app.sidebar,
  device: state => state.app.device,
  token: state => state.user.token,
  avatar: state => state.user.avatar,
  name: state => state.user.name,
  menus: state => state.user.menus,
  visitedViews: state => state.tagsView.visitedViews, //這個(gè)是新增的
  cachedViews: state => state.tagsView.cachedViews,  //這個(gè)是新增的
}
export default getters

六、修改 vue-admin-template\src\store\index.js

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import tagsView from './modules/tagsView' //這個(gè)是新增的

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    app,
    settings,
    user,
    tagsView,  //這個(gè)是新增的
  },
  getters
})

export default store

七锐秦、修改vue-admin-template\src\settings.js 添加

module.exports = {

  title: 'Vue Admin Template',
  tagsView: true,  //這個(gè)是新增的  設(shè)置為根據(jù)值咪奖,是否選擇加載。
  /**
   * @type {boolean} true | false
   * @description Whether fix the header
   */
  fixedHeader: false,

  /**
   * @type {boolean} true | false
   * @description Whether show the logo in sidebar
   */
  sidebarLogo: false
}

八农猬、修改vue-admin-template\src\store\modules\settings.js

import defaultSettings from '@/settings'

const { showSettings, fixedHeader, sidebarLogo, tagsView } = defaultSettings  //tagsView 這個(gè)是新增的

const state = {
  showSettings: showSettings,
  tagsView: tagsView,   //tagsView 這個(gè)是新增的
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  }
}

const actions = {
  changeSetting ({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}


九赡艰、解決控制臺(tái)報(bào)錯(cuò):
1、刪除vue-admin-template\src\layout\components\TagsView\index.vue中routes方法(因?yàn)闆]有用到權(quán)限校驗(yàn))

  computed: {
    visitedViews () {
      return this.$store.state.tagsView.visitedViews
    },
    // routes() {
    //   return this.$store.state.permission.routes   //把這個(gè)注釋掉
    // }
  },

2斤葱、遍歷標(biāo)簽時(shí)可能控制臺(tái)報(bào)錯(cuò)
filterAffixTags (routes, basePath = '/') {
let tags = []
//判斷是否為空慷垮,否則控制臺(tái)會(huì)報(bào)錯(cuò) 這個(gè)是新增的
if (this.routes) {
routes.forEach(route => {
if (route.meta && route.meta.affix) {
const tagPath = path.resolve(basePath, route.path)
tags.push({
fullPath: tagPath,
path: tagPath,
name: route.name,
meta: { ...route.meta }
})
}
if (route.children) {
const tempTags = this.filterAffixTags(route.children, route.path)
if (tempTags.length >= 1) {
tags = [...tags, ...tempTags]
}
}
})
}
return tags
},

最后如果你要固定住tagsView,那么這里你需要設(shè)置下。


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末揍堕,一起剝皮案震驚了整個(gè)濱河市料身,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌衩茸,老刑警劉巖芹血,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡幔烛,警方通過查閱死者的電腦和手機(jī)啃擦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饿悬,“玉大人令蛉,你說我怎么就攤上這事〗铺瘢” “怎么了珠叔?”我有些...
    開封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長弟劲。 經(jīng)常有香客問我祷安,道長,這世上最難降的妖魔是什么兔乞? 我笑而不...
    開封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任汇鞭,我火速辦了婚禮,結(jié)果婚禮上报嵌,老公的妹妹穿的比我還像新娘虱咧。我一直安慰自己熊榛,他們只是感情好锚国,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著玄坦,像睡著了一般血筑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上煎楣,一...
    開封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天豺总,我揣著相機(jī)與錄音,去河邊找鬼择懂。 笑死喻喳,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的困曙。 我是一名探鬼主播表伦,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼慷丽!你這毒婦竟也來了蹦哼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤要糊,失蹤者是張志新(化名)和其女友劉穎纲熏,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡局劲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年勺拣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鱼填。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡宣脉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剔氏,到底是詐尸還是另有隱情塑猖,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布谈跛,位于F島的核電站羊苟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏感憾。R本人自食惡果不足惜蜡励,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望阻桅。 院中可真熱鬧凉倚,春花似錦、人聲如沸嫂沉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽趟章。三九已至杏糙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蚓土,已是汗流浹背宏侍。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蜀漆,地道東北人谅河。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像确丢,于是被迫代替她去往敵國和親绷耍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容