vuex的基本使用

一、Vuex是什么?

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

二、vuex的5个核心属性:

1. state:vuex的基本数据,用来存储变量,可以把它是为vue中的data。

2. getters:可以把它是为vue中的computed,计算属性,可以对state中的变量进行处理。

3. mutations更改 Vuex 的 store 中的状态的唯一方法是提交 mutation,可以把它视为vue中的methods。

  • mutation必须是同步的,异步使用action。
  • mutation使用 store.commit 方法触发。

4. actions:它类似于上面的mutations,但是它又跟mutations有所不同。

  • action 提交的是 mutation,而不是直接变更状态。
  • action 可以包含任意异步操作。
  • action通过 store.dispatch 方法触发。

5. modules:模块,当我们的应用很大时,所有的状态都集中在一个store对象里,显得臃肿不堪,这时可以将store分割到模块,每个模块都有自己的state、getters、mutations、actions。

在项目中常用的:this.$store.state、this.$store.getters、this.$store.dispatch、this.$store.commit

三、vuex的使用

1. 首先安装vuex的依赖

1 npm install vuex --save

2. 在main.js文件中引入vuex

1 import Vue from "vue"
2 import Vuex from "vuex"
3 Vue.use(Vuex)

3. 在main.js文件的同级创建一个store.js文件

 1 import Vue from "vue"
 2 import Vuex from "vuex"
 3 Vue.use(Vuex)
 4 export default new Vuex.Store({
 5   state:{
 6     name:"初始的name"
 7   },
 8   getters:{
 9     // 第一个参数是state,第二个是getters
10     dealName(state,getters){
11       return "处理一下的name,值为:"+state.name
12     }
13   },
14   mutations:{
15     // 第一个参数是state,第二个参数为提交载荷,emmm,赋值的新值
16     setName(state,payload){
17        state.name = payload
18     }
19   },
20   actions:{
21     // 第一个参数为对象,与store 实例具有相同方法和属性的 context 对象,第二个参数与mutation一样
22     setNameAction({commit},payload){
23       commit("setName",payload)
24     }
25   }
26 })

4. 在main.js文件中引入store.js

1  import Vuex from "vuex";
2  import store from "./store";
3  Vue.use(Vuex);
4  new Vue({
5   render: h => h(App),
6   store
7  }).$mount('#app');

5. 在组件中使用store

 1 <template>
 2   <div>
 3 
 4     <div>name:{{name}}</div>
 5     <div>getter处理的name:{{dealName}}</div>
 6     <button @click="changename">改变name</button>
 7   
 8   </div>
 9 </template>
10 <script>
11 export default {
12   name: "index",
13   computed: {
14     name() {
15       return this.$store.state.name;
16     },
17     dealName() {
18       return this.$store.getters.dealName;
19     }
20   },
21   created() {
22     console.log(this.$store);
23   },
24   methods: {
25     changename() {
26     // 下面两种方式都可以改变state中name的值,分别是对应action和mutation
27       // this.$store.dispatch("setNameAction", "action改变name");
28       this.$store.commit("setName", "mutation改变name");
29     }
30   }
31 };
32 </script> 

 

 

 6. 将store分割成模块,修改store.js文件

 1 import Vue from "vue";
 2 import Vuex from "vuex";
 3 Vue.use(Vuex);
 4 export default new Vuex.Store({
 5     // 以下store被分割成2个模块,user、application
 6     modules: {
 7         user: {
 8             state: {
 9                 name: "初始的name"
10             },
11             getters: {
12                 // 第一个参数是state,第二个是getters
13                 dealName(state) {
14                     return "处理一下的name,值为" + state.name;
15                 }
16             },
17             mutations: {
18                 // 第一个参数是state,第二个参数为提交载荷,emmm,赋值的新值
19                 setName(state, payload) {
20                     state.name = payload;
21                 }
22             },
23             actions: {
24                 // 第一个参数为对象,与store 实例具有相同方法和属性的 context 对象,第二个参数与mutation一样
25                 setNameAction({ commit }, payload) {
26                     commit("setName", payload);
27                 }
28             }
29         },
30         application: {
31             state: {
32                 title: "初始标题"
33             },
34             getters: {
35                 // 第一个参数是state,第二个是getters
36                 dealTitle(state) {
37                     // 虽然getter不能接受外面传进的参数,但是我们可以将返回值处理成方法去接受参数
38                     return (type) => {
39                         return type + state.title;
40                     };
41                 }
42             },
43             mutations: {
44                 // 第一个参数是state,第二个参数为提交载荷,emmm,赋值的新值
45                 setTitle(state, payload) {
46                     state.name = payload;
47                 }
48             },
49             actions: {
50                 // 第一个参数为对象,与store 实例具有相同方法和属性的 context 对象,第二个参数与mutation一样
51                 setTitleAction({ commit }, payload) {
52                     commit("setTitle", payload);
53                 }
54             }
55         }
56     }
57 
58 });

7. 修改一下组件的使用

 1 <template>
 2   <div>
 3     <div>name:{{name}}</div>
 4     <div>getter处理的name:{{dealName}}</div>
 5     <button @click="changename">改变name</button>
 6     <div>title:{{title}}</div>
 7     <div>getter处理的title:{{dealTitle}}</div>
 8   </div>
 9 </template>
10 <script>
11 export default {
12   name: "index",
13   computed: {
14     name() {
15       // name是user模块中的,因此需要user.name
16       return this.$store.state.user.name;
17     },
18     dealName() {
19       return this.$store.getters.dealName;
20     },
21     title() {
22       return this.$store.state.application.title;
23     },
24     dealTitle() {
25       // dealTitle返回的是个方法,我们可以传参
26       return this.$store.getters.dealTitle("demo应用的");
27     }
28   },
29   created() {
30     console.log(this.$store);
31   },
32   methods: {
33     changename() {
34       // this.$store.dispatch("setNameAction", "action改变name");
35       this.$store.commit("setName", "mutation改变name");
36     }
37   }
38 };
39 </script>

 

posted @ 2020-05-18 14:33  蛙仔  阅读(936)  评论(1编辑  收藏  举报