uni-app已经内置了vuex,所以只要正确引入就好了。

1、根目录创建store文件存放vuex

2、创建index.js

导入vue和vuex

import Vue from 'vue'
import Vuex from 'vuex'

  

Vue.use(Vuex)

  

const store = new Vuex.Store({
    state: {
		city:'城市名',
		login:false
	},
    mutations: {
		ShowGood(){
			console.log('that is good!!!')
		}
	},
    actions: {}
})

 

export default store

 

3、main.js入口文件

// 引入store
import store from './store'
// 把vuex定义成全局组件
Vue.prototype.$store = store

 

导入辅助函数

import {
        mapState,
        mapMutations
    } from 'vuex'

 

 

 

 使用

computed: {
            ...mapState(['login', 'city'])
        },    
methods: {
            change(e) {
                this.current = e.detail.current;
            },
            ...mapMutations(['ShowGood'])
        }

 

posted on 2020-07-30 17:05  京鸿一瞥  阅读(1744)  评论(0)    收藏  举报