uniapp内置了vueX,不用自己安装,直接引入就好了。 nuiapp内置vueX文档说明 1、新建 store 文件夹,创建index.js文件
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
'phone': {
brand: null,
model: null,
},
'page': 0,
},
mutations: {
getPage(state, newValue) {
state.page = newValue;
},
getPhone(state, newValue) {
state.phone.brand = newValue.brand;
state.phone.model = newValue.model;
}
},
actions: {
setPage(context, value) {
context.commit('getPage', value);
},
setPhone(context, value) {
let phone = {
brand: value.brand,
model: value.model
}
context.commit('getPhone', phone);
},
},
modules: {}
})
export default store
<template>
<text class="text">{{phone.model}}</text>
</template>
<script>
export default {
onLaunch: function() {
const _this = this;
uni.getSystemInfo({
success: function (res) {
console.log(res.brand);
console.log(res.model);
_this.$store.dispatch('setPhone', {brand: res.brand, model: res.model});
}
});
},
computed: {
...mapState(['phone']),
}
}
</script>
<template>
<text class="text">{{page}}</text>
</template>
<script>
import { mapState } from 'vuex'
export default {
data() {
return{}
},
computed:{
...mapState(['page']),
},
methods: {
Jump(ind) {
this.$store.dispatch('setPage', ind);
}
},
}
</script>
|