目录

此时运行的是npm install vuex -S 对应的版本是4.0.2 因为和vuex不兼容 所以会报错

换成npm i -S vuex@3.6.2


就能用了
- import Vue from 'vue'
- import Vuex from 'vuex'
- import state from './state'
- import getters from './getters'
- import actions from './actions'
- import mutations from './mutations'
- Vue.use(Vuex)
- const store = new Vuex.Store({
- state,
- getters,
- actions,
- mutations
- })
-
- export default store
- import Vue from 'vue'
- import Router from 'vue-router'
- import HelloWorld from '@/components/HelloWorld'
- import AppMain from '@/components/AppMain'
- import LeftNav from '@/components/LeftNav'
- import TopNav from '@/components/TopNav'
- import Login from '@/views/Login'
- import Reg from '@/views/Reg'
- import Articles from '@/views/sys/Articles'
- import VuexPage1 from '@/views/sys/VuexPage1'
-
-
-
- Vue.use(Router)
-
- export default new Router({
- routes: [{
- path: '/',
- name: 'Login',
- component: Login
- },
- {
- path: '/Login',
- name: 'Login',
- component: Login
- },
- {
- path: '/Reg',
- name: 'Reg',
- component: Reg
- },
- {
- path: '/AppMain',
- name: 'AppMain',
- component: AppMain,
- children: [{
- path: '/LeftNav',
- name: 'LeftNav',
- component: LeftNav
- },
- {
- path: '/TopNav',
- name: 'TopNav',
- component: TopNav
- }
- ,{
- path: '/sys/Articles',
- name: 'Articles',
- component: Articles
- }
- ,{
- path: '/sys/VuexPage1',
- name: 'VuexPage1',
- component: VuexPage1
- }
- ]
- }
- ]
- })
- // 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'
- //process.env.MOCK 为false,那么require('@/mock')不执行 process.env.MOCK 在生产环境下为false
- //process.env.MOCK && require('@/mock') 因为与后台建立连接,不是用mock模拟数据了
- import ElementUI from 'element-ui' // 新添加 1
- import 'element-ui/lib/theme-chalk/index.css' // 新添加 2 ,避免后期打包样式不同,要放在import App from './App'; 之前
- import App from './App'
- import router from './router'
- import axios from '@/api/http' // #vue项目对axios的全局配置
- //import axios from 'axios'
- import VueAxios from 'vue-axios'
- import store from './store'
-
- Vue.use(ElementUI) // 新添加 3 使用ElementUI
- Vue.use(VueAxios,axios)
- Vue.config.productionTip = false
-
- /* eslint-disable no-new */
- new Vue({
- el: '#app',
- data(){
- return{
- Bus:new Vue({//总线
-
- })
- }
- },
- router,
- store,
- components: { App },
- template: '
' - })
- export default {
- resName:'小陈餐馆'
- };
- <template>
- <div>
- <p>欢迎来到{{msg}}p>
- div>
- template>
-
- <script>
- export default {
- name: 'VuexPage1',
- data () {
- return {
- }
- },
- computed:{
- msg(){
- //从vue的state文件中获取值
- return this.$store.state.resName;
- }
- }
- }
- script>
-
- <style>
- style>

- export default {//修改变量值 同步
- setResName:(state,payload)=>{
- //state对象就对应了state.js中的对象
- //payload载荷对应的传递的Jason对象参数
- state.resName = payload.resName;
- }
- };
- export default {
- getResName:(state)=> {
- return state.resName;
- }
- };
- <div>
- <p>欢迎来到{{msg}}p>
- <button @click="buy">盘它button>
- div>
-
- <script>
- export default {
- name: 'VuexPage1',
- data () {
- return {
- }
- },
- methods:{
- buy(){
- //通过commit方法会调用mutations.js文件中定义好的方法
- this.$store.commit("setResName",{
- resName:'KFC'
- })
- }
- },
- computed:{
- msg(){
- //从vue的state文件中获取值
- // return this.$store.state.resName; 不推荐不安全
- //通过getters.js获取state.js中定义的变量值
- return this.$store.getters.getResName;
- }
- }
- }
- script>
-
- <style>
- style>


- <div>
- <p>页面2:欢迎来到{{msg}}p>
- div>
-
- <script>
- export default {
- name: 'VuexPage2',
- data () {
- return {
- }
- },
- computed:{
- msg(){
- //从vue的state文件中获取值
- // return this.$store.state.resName; 不推荐不安全
- //通过getters.js获取state.js中定义的变量值
- return this.$store.getters.getResName;
- }
- }
- }
- script>
-
- <style>
- style>
- import Vue from 'vue'
- import Router from 'vue-router'
- import HelloWorld from '@/components/HelloWorld'
- import AppMain from '@/components/AppMain'
- import LeftNav from '@/components/LeftNav'
- import TopNav from '@/components/TopNav'
- import Login from '@/views/Login'
- import Reg from '@/views/Reg'
- import Articles from '@/views/sys/Articles'
- import VuexPage1 from '@/views/sys/VuexPage1'
- import VuexPage2 from '@/views/sys/VuexPage2'
-
-
-
- Vue.use(Router)
-
- export default new Router({
- routes: [{
- path: '/',
- name: 'Login',
- component: Login
- },
- {
- path: '/Login',
- name: 'Login',
- component: Login
- },
- {
- path: '/Reg',
- name: 'Reg',
- component: Reg
- },
- {
- path: '/AppMain',
- name: 'AppMain',
- component: AppMain,
- children: [{
- path: '/LeftNav',
- name: 'LeftNav',
- component: LeftNav
- },
- {
- path: '/TopNav',
- name: 'TopNav',
- component: TopNav
- }
- ,{
- path: '/sys/Articles',
- name: 'Articles',
- component: Articles
- }
- ,{
- path: '/sys/VuexPage1',
- name: 'VuexPage1',
- component: VuexPage1
- }
- ,{
- path: '/sys/VuexPage2',
- name: 'VuexPage2',
- component: VuexPage2
- }
- ]
- }
- ]
- })


- export default {
- setResNameAsync: (context, payload) => {
- //异步修改值 在异步方法中调了同步方法
- //context是指vuex的上下文相当于this.$store
- setTimeout(function(){//6秒后执行
- context.commit("setResName",payload);
- },6000);
- }
- };
- <div>
- <p>页面1:欢迎来到{{ msg }}p>
- <button @click="buy">盘它button>
- <button @click="buyAsync">最终店长button>
- div>
-
- <script>
- export default {
- name: "VuexPage1",
- data() {
- return {};
- },
- methods: {
- buy() {
- //通过commit方法会调用mutations.js文件中定义好的方法
- this.$store.commit("setResName", {
- resName: "KFC"
- });
- },
- buyAsync() {
- this.$store.dispatch("setResNameAsync", {
- resName: "华莱士"
- });
- }
- },
- computed: {
- msg() {
- //从vue的state文件中获取值
- // return this.$store.state.resName; 不推荐不安全
- //通过getters.js获取state.js中定义的变量值
- return this.$store.getters.getResName;
- }
- }
- };
- script>
-
- <style>style>

点击最终店长 等6秒变为如下图

点击角色管理

- <div>
- <p>页面1:欢迎来到{{ msg }}p>
- <button @click="buy">盘它button>
- <button @click="buyAsync">最终店长button>
- div>
-
- <script>
- export default {
- name: "VuexPage1",
- data() {
- return {};
- },
- methods: {
- buy() {
- //通过commit方法会调用mutations.js文件中定义好的方法
- this.$store.commit("setResName", {
- resName: "KFC"
- });
- },
- buyAsync() {
- this.$store.dispatch("setResNameAsync", {
- resName: "华莱士",
- _this:this
- });
- }
- },
- computed: {
- msg() {
- //从vue的state文件中获取值
- // return this.$store.state.resName; 不推荐不安全
- //通过getters.js获取state.js中定义的变量值
- return this.$store.getters.getResName;
- }
- }
- };
- script>
-
- <style>style>
- export default {
- setResNameAsync: (context, payload) => {
- //异步修改值 在异步方法中调了同步方法
- //context是指vuex的上下文相当于this.$store
- setTimeout(function(){//6秒后执行
- context.commit("setResName",payload);
- },6000);
-
- //vuex后台交互
- let _this = payload._this;
- let url = _this.axios.urls.SYSTEM_MENU_TREE;
- _this.axios.post(url,{}).then(r=>{
- console.log(r);
- }).catch(e=>{
-
- });
- }
- };

注意:必须是_this 而不能是this,若直接用this会拿不到axios
vuex中默认情况下是不能使用vue实例,想要请求后台接口,必须将vue实例当初参数从组件那一方传递过来
总结:
①下载vuex的依赖 npm install vuex -S 是最新版本 4.0.2
我们需要是npm i -S vuex@3.6.2
②导入vuex核心的四个组件,通过index.js加载进来
③将vuex对应的index.js挂载到main.js的vue实例中
④测试vuex的存储变量的功能