|
@@ -1,10 +1,110 @@
|
|
|
<template>
|
|
|
- <div>新内容</div>
|
|
|
+ <div>
|
|
|
+ <h1>新内容</h1>
|
|
|
+ <!--
|
|
|
+ State
|
|
|
+ 1.在标签中调用state
|
|
|
+ $store.state.xxxx
|
|
|
+ 2.在方法中调用
|
|
|
+ this.$store.state.xxx
|
|
|
+ 3.辅助函数
|
|
|
+ mapState
|
|
|
+ 必须要引入
|
|
|
+ 使用:...mapState(["xxx"],["xxx"],....)
|
|
|
+ -->
|
|
|
+ <!--
|
|
|
+ Mutations 同步
|
|
|
+ 1.this.$store.commit("store中mutations里定义的方法",传参)
|
|
|
+ 必须要引入
|
|
|
+ 2.辅助函数
|
|
|
+ 解构后 ...mapMutations(["xxx"],["xxx"],....)
|
|
|
+ 直接调用 this.xxx()
|
|
|
+ -->
|
|
|
+ <!--
|
|
|
+ Actions:
|
|
|
+ 定义:方法名(context){
|
|
|
+ context.commit("同步方法名")
|
|
|
+ }
|
|
|
+ 调用: this.$store.dispatch(“定义的方法”)
|
|
|
+ 辅助函数:
|
|
|
+ 必须要引入
|
|
|
+ -->
|
|
|
+ <!--
|
|
|
+ Getters
|
|
|
+ 1.在标签中调用getters
|
|
|
+ $store.getters.xxxx
|
|
|
+ 2.在方法中调用
|
|
|
+ this.$store.getters.xxx
|
|
|
+ 3.辅助函数
|
|
|
+ mapGetters
|
|
|
+ 必须要引入
|
|
|
+ 使用:...mapGetters(["xxx"],["xxx"],....)
|
|
|
+
|
|
|
+ -->
|
|
|
+ <p>今天的天气:{{$store.state.weather}}</p>
|
|
|
+ <p>我们在:{{$store.state.address}}</p>
|
|
|
+ {{aa}},{{bb}}
|
|
|
+ <br>
|
|
|
+ {{address}}/{{weather}}
|
|
|
+ <h3>数量:{{count}}</h3>
|
|
|
+ <button @click="add">添加</button>
|
|
|
+ <br>
|
|
|
+ <br>
|
|
|
+ <br>
|
|
|
+ <button @click="reduceMain">减少</button>
|
|
|
+ <br>
|
|
|
+ <br>
|
|
|
+ <br>
|
|
|
+ <button @click="changeMain">改变</button>
|
|
|
+ <br>
|
|
|
+ <br>
|
|
|
+ 1.{{cc}}/
|
|
|
+ 2.{{$store.getters.newCount}}/
|
|
|
+ 3.{{newCount}}
|
|
|
+ </div>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
+import {mapState,mapMutations,mapActions,mapGetters} from 'vuex';
|
|
|
export default {
|
|
|
- name:"New"
|
|
|
+ name:"New",
|
|
|
+ data() {
|
|
|
+ return{
|
|
|
+ // aa:"",
|
|
|
+ // bb:""
|
|
|
+ cc:""
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // state内容 辅助函数在计算属性中调用
|
|
|
+ computed:{
|
|
|
+ ...mapState(["address","weather","count"]),
|
|
|
+ ...mapGetters(["newCount"])
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ this.aa = this.$store.state.weather;
|
|
|
+ this.bb = this.$store.state.address;
|
|
|
+ },
|
|
|
+ methods:{
|
|
|
+ add() {
|
|
|
+ // mutations => commit
|
|
|
+ this.$store.commit("addCount",10);
|
|
|
+ },
|
|
|
+ // ...mapMutations(["reduce"]),
|
|
|
+ ...mapActions(["asyncAdd"]),
|
|
|
+ reduceMain() {
|
|
|
+ // 同步方法
|
|
|
+ // this.reduce();
|
|
|
+ // this.$store.commit("reduce")
|
|
|
+ // 异步方法
|
|
|
+ // this.$store.dispatch("asyncAdd");
|
|
|
+ this.asyncAdd();
|
|
|
+
|
|
|
+ },
|
|
|
+ changeMain() {
|
|
|
+ this.cc = this.$store.getters.newCount;
|
|
|
+ console.log(this.$store.getters.newCount)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
</script>
|