|
|
@@ -1,40 +1,54 @@
|
|
|
<!-- eslint-disable vue/multi-word-component-names -->
|
|
|
<template>
|
|
|
<div class="home">
|
|
|
- <!-- state -->
|
|
|
+ <!-- state -->
|
|
|
<h1>我叫{{ name1 }}</h1>
|
|
|
- <h1>我叫{{ $store.state.userName }}</h1>
|
|
|
+ <h1>我叫{{ $store.state.a.userName }}</h1>
|
|
|
<h1>我叫{{ userName }},今年{{ age }}岁</h1>
|
|
|
<!-- getters -->
|
|
|
<h1>数量{{ count1 }}</h1>
|
|
|
- <h1>数量{{ $store.getters.doubleCount}}</h1>
|
|
|
+ <h1>数量{{ $store.getters['a/doubleCount'] }}</h1>
|
|
|
<h1>数量{{ doubleCount }}</h1>
|
|
|
+ <button @click="changeCount">增加</button>
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
-import { mapState,mapGetters } from 'vuex';
|
|
|
+import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
|
|
|
export default {
|
|
|
- name:"Home",
|
|
|
+ name: "Home",
|
|
|
data() {
|
|
|
return {
|
|
|
- name1:"",
|
|
|
- count1:""
|
|
|
- }
|
|
|
+ name1: "",
|
|
|
+ count1: "",
|
|
|
+ };
|
|
|
},
|
|
|
created() {
|
|
|
- this.name1 = this.$store.state.userName;
|
|
|
- this.count1 = this.$store.getters.doubleCount;
|
|
|
- console.log("首页",this.$store.getters.doubleCount)
|
|
|
+ console.log("首页", this.$store);
|
|
|
+ this.name1 = this.$store.state.a.userName;
|
|
|
+ this.count1 = this.$store.getters['a/doubleCount'];
|
|
|
// console.log(mapState)
|
|
|
},
|
|
|
- computed:{
|
|
|
- ...mapState(['userName','age']),
|
|
|
- ...mapGetters(['doubleCount'])
|
|
|
- }
|
|
|
-}
|
|
|
+ computed: {
|
|
|
+ ...mapState('a',["userName", "age"]),
|
|
|
+ ...mapGetters('a',["doubleCount"]),
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ // ...mapMutations('a',["addCount"]),
|
|
|
+ ...mapActions('a',['asyncCount']),
|
|
|
+ changeCount() {
|
|
|
+ // 异步
|
|
|
+ // this.$store.dispatch('a/asyncCount',2)
|
|
|
+ this.asyncCount(3)
|
|
|
+
|
|
|
+
|
|
|
+ // 同步
|
|
|
+ // this.addCount(10);
|
|
|
+ // this.$store.commit('a/addCount',20)
|
|
|
+ },
|
|
|
+ },
|
|
|
+};
|
|
|
</script>
|
|
|
|
|
|
<style>
|
|
|
-
|
|
|
</style>
|