首页 技术 正文
技术 2022年11月8日
0 收藏 400 点赞 1,259 浏览 1505 个字

1、vuex 配置

//vuex的配置
//注意Store是大写
const store = new Vuex.Store({
//数据保存
state: {
show: false,
count: 0,
list: [1, 5, 8, 10, 30, 50]
},
mutations: {
increase(state, n = 1) {
state.count += n;
},
decrease(state, n = 1) {
state.count -= n;
},
switch_dialog (state) { // 这里的state对应着上面这个state
state.show = state.show ? false : true
// 你还可以在这里执行其他的操作改变state } },
getters: {
filteredList: state
=> {
return state.list.filter(item => item < 31
);
}
},

actions:{
asyncIncrease(context){
context.commit('increase');
},
switch_dialog123 (context) { // 这里的context和我们使用的$store拥有相同的对象和方法
context.commit('switch_dialog')
// 你还可以在这里触发其他的mutations方法 }
}
});

2、mapGetters使用

<template>
<div>
{{count}}
<button @click="handleIncrease">+5</button>
<button @click="handleAsyncIncrease">-5</button> {{filteredList}}
<button @click="handleRouter">跳转到 HelloWorld3</button>
<button @click="showRouter">展示路由</button>
</div>
</template><script>
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'

export default {
name: 'HelloWorld2',
computed: {
// count(){
// return this.$store.state.count;
// },
// filteredList() {
// return this.$store.getters.filteredList;
// },

...mapState({
count: state => state.count
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'filteredList'

])

},
methods: {
handleIncrease() {
this.$store.commit('increase', 5);
},
handleDecrease() {
this.$store.commit('decrease', 5);
},
handleAsyncIncrease() {
this.$store.dispatch('asyncIncrease');
},
handleRouter() {
this.$router.push('/HelloWorld3');
},
showRouter() {
console.log(this.$router);
console.log(this.$router.push);
}
}
};
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,912
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,436
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,251
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,063
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,694
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,732