|
@@ -1,32 +1,72 @@
|
|
|
<template>
|
|
|
- <div id="app">
|
|
|
- <nav>
|
|
|
- <router-link to="/">Home</router-link> |
|
|
|
- <router-link to="/about">About</router-link>
|
|
|
- </nav>
|
|
|
- <router-view/>
|
|
|
- </div>
|
|
|
-</template>
|
|
|
+ <!--
|
|
|
+ state:数据存储
|
|
|
+ 使用方法:
|
|
|
+ 1.在生命周期中 通过this.$store.state.xxxx
|
|
|
+ 2.在计算属性中 通过mapState辅助函数 进行结构 注意:从vueX中引入辅助函数
|
|
|
+ 3.在template模板中,通过$store.state.xxx
|
|
|
|
|
|
-<style lang="scss">
|
|
|
-#app {
|
|
|
- font-family: Avenir, Helvetica, Arial, sans-serif;
|
|
|
- -webkit-font-smoothing: antialiased;
|
|
|
- -moz-osx-font-smoothing: grayscale;
|
|
|
- text-align: center;
|
|
|
- color: #2c3e50;
|
|
|
-}
|
|
|
+ getters:相当于computed
|
|
|
+ 使用方法:
|
|
|
+ 1.在生命周期中 通过this.$store.getters.xxxx
|
|
|
+ 2.在计算属性中 通过mapGetters辅助函数 进行结构 注意:从vueX中引入辅助函数
|
|
|
+ 3.在template模板中,通过$store.getters.xxx
|
|
|
|
|
|
-nav {
|
|
|
- padding: 30px;
|
|
|
-
|
|
|
- a {
|
|
|
- font-weight: bold;
|
|
|
- color: #2c3e50;
|
|
|
+ mutations 同步方法
|
|
|
+ 使用方法:
|
|
|
+ 1.再方法中 直接使用this.$store.commit("调用方法",参数1,参数2...)
|
|
|
+ 2.引入辅助函数 mapMutations,在哪个方法前使用 就直接结构...mapMutations(['xxx']),使用this.xxx()
|
|
|
+ actions 异步方法
|
|
|
+ 使用方法:
|
|
|
+ 1.再方法中 直接使用this.$store.dispatch("调用方法")
|
|
|
+ 2.引入辅助函数 mapActions,在哪个方法前使用 就直接结构...mapActions(['xxx']),使用this.xxx()
|
|
|
+ 在store库中
|
|
|
+ 封装异步方法 要注意context参数
|
|
|
+ 调用同步 context.commit("方法")
|
|
|
+ -->
|
|
|
+ <div class="app">
|
|
|
+ <h1>VueX</h1>
|
|
|
+ <p>我叫{{names}},今年{{$store.state.age}}岁,来自{{ address }}</p>
|
|
|
+ <p>新闻数量:{{newsCount}}</p>
|
|
|
+ <p>新闻数量:{{$store.getters.newCount}}</p>
|
|
|
+ <p>新闻数量:{{newCount}}</p>
|
|
|
+ <button @click="add">加加</button>
|
|
|
+ <button @click="reduce">减减</button>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
|
|
|
- &.router-link-exact-active {
|
|
|
- color: #42b983;
|
|
|
+<script>
|
|
|
+import {mapState,mapGetters,mapMutations,mapActions} from 'vuex';
|
|
|
+export default {
|
|
|
+ data(){
|
|
|
+ return {
|
|
|
+ names:"",
|
|
|
+ newsCount:""
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created(){
|
|
|
+ this.newsCount = this.$store.getters.newCount;
|
|
|
+ this.names = this.$store.state.name;
|
|
|
+ },
|
|
|
+ computed:{
|
|
|
+ ...mapState(["address"]),
|
|
|
+ ...mapGetters(["newCount"])
|
|
|
+ },
|
|
|
+ methods:{
|
|
|
+ add() {
|
|
|
+ this.$store.commit('addCount',10)
|
|
|
+ },
|
|
|
+ ...mapMutations(["reduceCount"]),
|
|
|
+ ...mapActions(['asyncReduce']),
|
|
|
+ reduce() {
|
|
|
+ this.asyncReduce()
|
|
|
+ // this.reduceCount();
|
|
|
+ // this.$store.dispatch("asyncReduce")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-</style>
|
|
|
+</script>
|
|
|
+
|
|
|
+<style>
|
|
|
+
|
|
|
+</style>
|