vue2 为 3 版本、vuex3 为 vuex4 版本。
npm install vuex@next --save
S E:\pdf1\vue-aadmin> npm install vuex@next --save
npm WARN ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN css-loader@2.0.2 requires a peer of webpack@^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN less-loader@5.0.0 requires a peer of less@^2.3.1 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN update-browserslist-db@1.0.10 requires a peer of browserslist@>= 4.21.0 but none is installed. You must install peer dependencies yourself.
npm WARN vuex@4.0.2 requires a peer of vue@^3.0.2 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
+ vuex@4.0.2
added 2 packages from 2 contributors in 10.942s
PS E:\pdf1\vue-aadmin>

npm install --save vuex@3
PS E:\pdf1\vue-aadmin> npm install --save vuex@3
npm WARN ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN css-loader@2.0.2 requires a peer of webpack@^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN less-loader@5.0.0 requires a peer of less@^2.3.1 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN update-browserslist-db@1.0.10 requires a peer of browserslist@>= 4.21.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
+ vuex@3.6.2
removed 1 package and updated 1 package in 7.58s

新建文件夹及文件:store/index.js
E:\pdf1\vue-aadmin\src\store\index.js
E:\pdf1\vue-aadmin\src\store\index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
}
})
export default store
E:\pdf1\vue-aadmin\src\main.js
// 省略其他
// 1. 导入store
import store from './store'
new Vue({
// 省略其他...
store // 2. 注入Vue实例
})
在任意组件中,通过 this.$store.state 来获取公共数据。
methods:{
add() {
console.log('add Event from info!')
console.log(this.$store.state)
console.log(this.$store.state.count)
store.commit('inscrease')
}
}


import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
// state: 用来保存所有的公共数据
state: {
nav:[
{
id:101,
name:"平台管理"
},
{
id:104,
name:"接入系统管理"
}
]
},
// 用它修改 state 的方法
mutations:{
navs(state, newUrl){
state.nav = newUrl
}
}
})
export default store
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 1. 导入store
import store from './store/index.js'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI, { size: 'small', zIndex: 3000 });
Vue.config.productionTip = false
// 导入字体图标
import './assets/fonts/iconfont.css'
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: ' '
})
<template>
<el-container class="nav-container">
<!-- 头部区域 -->
<el-header>
<div>
<img width="50px" src="../assets/logo.png" alt="" />
<span @click="fun_systemMenges">平台管理</span>
<span class="toggle-button" @click="toggleCollapse">
<i class="el-icon-s-fold"></i>
</span>
</div>
<el-button type="info">退出</el-button>
</el-header>
<!-- 页面主体区域 -->
<el-container>
<!-- 侧边栏 -->
<el-aside :width="isCollapse ? '64px' : '200px'">
<!-- 侧边栏菜单区域 -->
<el-menu
background-color="#333744"
text-color="#fff"
active-text-color="#409eff"
:unique-opened="false"
:collapse="isCollapse"
:collapse-transition="false"
:router="true"
:default-active="activePath"
>
<!-- 一级菜单 -->
<el-submenu
:index="item.id + ''"
v-for="item in menulist"
:key="item.id"
>
<template slot="title">
<i :class="iconsObj[item.id]"></i>
<span>{{ item.authName }}</span>
</template>
<!-- 二级菜单 -->
<el-menu-item
:index="subitem.path"
v-for="subitem in item.children"
:key="subitem.id"
@click="savaNavState(subitem.path,item,subitem)"
>
<template slot="title">
<i class="el-icon-menu"></i>
<span>{{ subitem.authName }}</span>
</template>
</el-menu-item>
</el-submenu>
</el-menu>
</el-aside>
<!-- 右侧内容主体 -->
<el-main>
<!-- 路由占位符 -->
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</template>
<script>
export default {
data() {
return {
// 左侧菜单
menulist: [
{
id: 101,
authName: "平台管理",
path: null,
children: [
{
id: 104,
authName: "接入系统管理",
path: "/system",
children: [],
},
],
},
{
id: 202,
authName: "系统权限管理",
path: "/HelloWorld",
children: [
{
id: 205,
authName: "权限管理",
path: "/HelloWorld",
children: [],
},
],
},
{
id: 302,
authName: "审计日志",
path: "/Welcome",
children: [
{
id: 305,
authName: "审计日志",
path: "/Welcome",
children: [],
},
],
},
],
iconsObj: {
// 一级菜单的icon图标
101: "el-icon-magic-stick",
202: "iconfont icon-users",
302: "iconfont icon-tijikongjian",
// 101: "iconfont icon-shangpin",
// 102: "iconfont icon-danju",
// 145: "iconfont icon-baobiao",
},
// 侧边栏是否折叠
isCollapse:false,
// 被激活的链接地址
activePath:""
};
},
created(){
this.activePath = window.sessionStorage.getItem("activePath");
// 公共数据传递导航数据
let getnav = JSON.parse(sessionStorage.getItem('nav'));
this.$store.commit('navs',getnav)
},
methods: {
// 折叠侧边栏
toggleCollapse(){
this.isCollapse = !this.isCollapse;
},
// 调整到首页
fun_systemMenges(){
this.$router.push({name:'NavMenu'})
window.sessionStorage.removeItem("activePath");
window.sessionStorage.removeItem("nav");
this.activePath = "/NavMenu";
},
// 侧边栏点击高亮
savaNavState(activePath,item,subitem){
window.sessionStorage.setItem("activePath",activePath);
this.activePath = activePath;
const nav=[
{
id:item.id,
name:item.authName
},
{
id:subitem.id,
name:subitem.authName
}
]
// 公共数据传递导航数据
this.$store.commit('navs',nav)
// console.log(JSON.stringify(nav))
window.sessionStorage.setItem("nav",JSON.stringify(nav));
}
},
};
</script>
<style lang="less" scoped>
.nav-container {
min-height: 100vh;
}
.el-header {
background-color: #373d41;
display: flex;
/* 均匀排列每个元素 */
justify-content: space-between;
/* 适用于所有的flex容器,单行时候垂直居中使用align-items: center。 */
align-items: center;
color: #fff;
/* 设置文章,嵌套写法 */
> div {
display: flex;
align-items: center;
span {
margin-left: 30px;
cursor: pointer;
}
.toggle-button {
margin-left: 35px;
cursor: pointer;
}
}
}
.el-aside {
background-color: #333744;
.el-menu {
border-right: none;
}
}
.el-main {
background-color: #eaedf1;
}
.iconfont {
margin-right: 10px;
}
</style>
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 1. 导入store
import store from './store/index.js'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI, { size: 'small', zIndex: 3000 });
Vue.config.productionTip = false
// 导入字体图标
import './assets/fonts/iconfont.css'
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: ' '
})