
如果你的项目添加完控制台报以下错误,那么请仔细阅读这篇文章,可以帮你解决这个问题
1 2
| **[Vue warn]: Error in mounted hook: "TypeError: this.$store.state.permission is undefined" TypeError: this.$store.state.permission is undefined**
|
第一步:下载vue-admin-template-master
注意如果vue-element-admin-master下载的是4.4.0版本
相应的vue-admin-template-master也要下载4.4.0版本,其他版本没有试过,不知道会不会报错
从vue-element-admin-master中复制如下文件夹和文件
vue-element-admin-master\src\layout\components\TagsView 文件夹
vue-element-admin-master\src\store\modules\tagsView.js
文件复制到vue-admin-template-master的对应位置
注意一点,网上有教程说permission.js这个文件也要复制,其实没必要,这个是VUE的权限控制插件,有需要的时候再复制过来也不迟
第二步:修改vue-admin-template-master\src\layout\components\AppMain.vue
1 2 3 4 5 6 7 8 9 10
| <template> <section class="app-main"> <transition name="fade-transform" mode="out-in"> //这个注释掉 <keep-alive :include="cachedViews"> //新增这个标签 <router-view :key="key" /> </keep-alive> </transition> </section> </template>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <script> export default { name: 'AppMain', computed: { key() { return this.$route.path }, cachedViews(){ return this.$store.state.tagsView.cachedViews } } } </script>
|
第三步:修改vue-admin-template-master\src\layout\components\index.js
1
| export { default as TagsView } from './TagsView'
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <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 /> <tagsView /> //在此处新增tagsView标签 </div> <app-main /> </div> </div> </template>
|
1 2 3 4 5 6 7 8 9 10 11
| import { Navbar, Sidebar, AppMain, TagsView } from './components' import ResizeMixin from './mixin/ResizeHandler'
export default { name: 'Layout', components: { Navbar, Sidebar, AppMain, TagsView },
|
第五步:在vue-admin-template-master\src\store\getters.js 文件中新增如下代码
1 2
| visitedViews: state => state.tagsView.visitedViews, cachedViews: state => state.tagsView.cachedViews
|
第六步:在vue-admin-template-master\src\store\index.js 文件中新增如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 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'
Vue.use(Vuex)
const store = new Vuex.Store({ modules: { app, settings, user, tagsView }, getters })
|
第七步:在vue-admin-template-master\src\settings.js 添加
第八步:在vue-admin-template-master\src\store\modules\settings.js 添加
1 2 3 4 5 6 7 8 9 10
| import defaultSettings, {tagsView} from '@/settings'
const { showSettings, fixedHeader, sidebarLogo } = defaultSettings
const state = { showSettings: showSettings, fixedHeader: fixedHeader, sidebarLogo: sidebarLogo, tagsView: tagsView }
|
1 2 3 4 5 6 7 8 9
| computed: { visitedViews() { return this.$store.state.tagsView.visitedViews },
},
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| filterAffixTags(routes, basePath = '/') { let tags = [] 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} }) }
|
第十步:在vue-admin-template-master\src\router\index.js
1 2 3 4 5 6 7 8 9 10 11 12 13
| { path: '/', component: Layout, redirect: '/dashboard', children: [{ path: 'dashboard', name: 'Dashboard', component: () => import('@/views/dashboard/index'), meta: { title: '首页', icon: 'dashboard', affix:true } }] },
|