|
|
@@ -0,0 +1,46 @@
|
|
|
+<template>
|
|
|
+ <div class="pagethree">
|
|
|
+ <h1>PageThree</h1>
|
|
|
+ <!-- 获取全局状态count的值 $store.state.状态名 -->
|
|
|
+ <!-- <h1>全局状态count的值为:{{ $store.state.count }}</h1> -->
|
|
|
+ <h1>全局状态count的值为:{{ count }}</h1>
|
|
|
+ <!-- getters 可以用来获取全局状态的值 $store.getters.计算属性名 -->
|
|
|
+ <!-- <h1>{{ $store.getters.valTest }}</h1> -->
|
|
|
+ <h1>{{ valTest }}</h1>
|
|
|
+ <button @click="changeCount">改变全局状态count的值</button>
|
|
|
+
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+<script>
|
|
|
+import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
|
|
|
+export default {
|
|
|
+ name: 'PageThree',
|
|
|
+ methods: {
|
|
|
+ changeCount() {
|
|
|
+ // 改变全局状态count的值
|
|
|
+ // commit 方法用来提交mutations方法
|
|
|
+ // commit 方法中可以传递参数 第一个参数是mutations方法的名称 从第二个参数开始是传递给mutations方法的参数
|
|
|
+ // this.$store.commit('countChange',100);
|
|
|
+ // this.countChange(100);
|
|
|
+ // actions 方法用来提交mutations方法 用来异步处理state状态值的变化
|
|
|
+ // this.$store.dispatch('countChangeAsync',100);
|
|
|
+ this.countChangeAsync(100);
|
|
|
+ },
|
|
|
+ ...mapMutations(['countChange']),
|
|
|
+ ...mapActions(['countChangeAsync']),
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ // 在js中使用 vuex 中的属性需要添加this
|
|
|
+ // 组件挂载完成 调用全局状态count 属性
|
|
|
+ console.log(this.$store.state.count);
|
|
|
+ // 组件挂载完成 调用全局状态的计算属性
|
|
|
+ console.log(this.$store.getters.valTest);
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ // mapState 方法可以将vuex 中的状态映射到组件的计算属性中 返回指定的状态值
|
|
|
+ ...mapState(['count']),
|
|
|
+ // mapGetters 方法可以将vuex 中的计算属性映射到组件的计算属性中 返回指定的计算属性值
|
|
|
+ ...mapGetters(['valTest']),
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|