vue-admin-template模板添加 tagsview


從vue-element-admin復制文件:

  • vue-admin-template\src\layout\components\TagsView 文件夾
  • vue-admin-template\src\store\modules\tagsView.js

vue-admin-template\src\layout\components\AppMain.vue新增以下內容:

<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
     <!-- <router-view :key="key" />-->
      <keep-alive :include="cachedViews">
        <router-view></router-view>
      </keep-alive>
    </transition>
  </section>
</template>

export default {
  name: 'AppMain',
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    }/*,
    key() {
      return this.$route.path
    }*/
  }
}
<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 TagsView } from './TagsView'

vue-admin-template\src\layout\index.vue

<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar class="sidebar-container" />
    <div class="main-container">
      <div :class="{'fixed-header':fixedHeader}">
        <navbar />
      </div>
      <tags-view />    <!-- 此處增加tag-->
      <app-main />
    </div>
  </div>
</template>
import { Navbar, Sidebar, AppMain, TagsView } from './components'
components: {
    Navbar,
    Sidebar,
    AppMain,
    TagsView
  },

修改 vue-admin-template\src\store\getters.js

visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews,

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

import tagsView from './modules/tagsView'

const store = new Vuex.Store({
  modules: {
    app,
    permission,
    settings,
    tagsView,
    user
  },
  getters
})

修改vue-admin-template\src\settings.js 添加

tagsView: true,

修改vue-admin-template\src\store\modules\settings.js

const { showSettings, tagsView, fixedHeader, sidebarLogo } = defaultSettings

const state = {
  showSettings: showSettings,
  tagsView: tagsView,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo
}

解決控制台報錯及常見需求

沒有用到權限校驗

判斷是否為空,否則控制台會報錯

循環引用報錯

Error in callback for watcher "$route": "TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Vue'
    |     property '$options' -> object with constructor 'Object'
    |     property 'router' -> object with constructor 'VueRouter'
    --- property 'app' closes the circle"
  • 修改vue-admin-template\src\store\modules\tagsView.js
const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (state.visitedViews.some(v => v.meta.title === view.meta.title)) return;
    let newView = {
      fullPath: view.fullPath,
      hash: view.hash,
      meta: { ...view.meta },
      name: view.name,
      params: { ...view.params },
      path: view.path,
      query: { ...view.query },
      title: view.title,
    }
    if (newView.meta.title) { // 沒有名字的不添加標簽 
      state.visitedViews.push(
        Object.assign({}, newView, {
          title: newView.meta.title
        })
      )
    }
  },

tabs-view 不可刪除

  • 加上 affix:true
{
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: '首頁', icon: 'dashboard', affix: true
        },
 },

刷新后標簽欄消失了

  • 在vue-admin-template\src\layout\components\tagsView\index.vue添加
  • mounted中執行
 beforeUnload() {
      // 監聽頁面刷新
      window.addEventListener("beforeunload", () => {
        // visitedViews數據結構太復雜無法直接JSON.stringify處理,先轉換需要的數據
        console.log(this.visitedViews,'this.visitedViews')
        let tabViews = this.visitedViews.map((item) => {
          return {
            fullPath: item.fullPath,
            hash: item.hash,
            meta: { ...item.meta },
            name: item.name,
            params: { ...item.params },
            path: item.path,
            query: { ...item.query },
            title: item.title,
          };
        });
        sessionStorage.setItem("tabViews", JSON.stringify(tabViews));
      });
      // 頁面初始化加載判斷緩存中是否有數據
      let oldViews = JSON.parse(sessionStorage.getItem("tabViews")) || [];
       console.log(oldViews,'this.visitedViews2')
      if (oldViews.length > 0) {
        this.$store.state.tagsView.visitedViews = oldViews;
      }
    },
mounted() {
    this.initTags();
     this.addTags();
    // 刷新頁面標簽不丟失
    this.beforeUnload();
  },

退出刪除標簽頁

  • 可以在路由全局守衛中判斷是否跳轉到登陸頁,然后將vue-admin-template\src\store\modules\tagsView.js中的visitedViews置為


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM