new Vuex.Store({})
state:{}
store.state.数据
mutations: {}
<div id="app"></div>
<template id="tpl">
<div>
<tip></tip>
<but></but>
</div>
</template>
<script>
var store = new Vuex.Store({
state:{
count:0
},
mutations:{
jia (state) {
state.count++
},
jian (state) {
state.count--
}
}
});
var vm = new Vue({
el:"#app",
template:"#tpl",
components:{
tip:{
template:"<div>{{$store.state.count}}</div>"
},
but:{
template:`
<div>
<input type="button" value="+" @click="$store.commit('jia')"/>
<input type="button" value="-" @click="$store.commit('jian')"/>
</div>
`
}
},
store
});
</script>
<script>
//我们从store里面获取的数据最好放到计算属性中
var store = new Vuex.Store({
state:{
count:0,
num1:1,
num2:2
},
mutations:{
jia (state) {
state.count++
},
jian (state) {
state.count--
}
}
});
var vm = new Vue({
el:"#app",
template:"#tpl",
components:{
tip:{
//创建计算属性
// computed:{
// count(){
// return this.$store.state.count;
// },
// num1(){
// return this.$store.state.num1;
// },
// num2(){
// return this.$store.state.num2;
// }
// },
//使用mapState辅助函数
//computed:Vuex.mapState({
// count:state=>state.count,
//num1:state=>state.num1,
//num2:state=>state.num2
//}),
//mapState 传一个字符串数组
computed:Vuex.mapState(['count' , 'num1' , 'num2']),
template:"<div>{{count}}{{num1}}{{num2}}</div>"
},
but:{
template:`
<div>
<input type="button" value="+" @click="$store.commit('jia')"/>
<input type="button" value="-" @click="$store.commit('jian')"/>
</div>
`
}
},
store
});
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有