uniapp vuex 的使用


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'])
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM