每天忙碌的日子,也不要忘记了好好生活 🏠

vuePress-theme-reco happylay 🐑    2020 - 2021
每天忙碌的日子,也不要忘记了好好生活 🏠 每天忙碌的日子,也不要忘记了好好生活 🏠

Choose mode

  • dark
  • auto
  • light
主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
author-avatar

happylay 🐑

34

文章

24

标签

主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
  • 2020

    • api 接口服务
    • dom渲染总结
    • eslint代码规范
    • promise 异步总结
    • scss总结
    • typescript 总结
    • vue2 常用配置
    • vue2总结
    • vue3总结
    • vuex总结
    • 响应式布局
    • 基础样式
    • 常用js写法
    • 常用npm类库
    • 常用样式
    • 样式卡片

vuex总结

vuePress-theme-reco happylay 🐑    2020 - 2021

vuex总结

happylay 🐑 2020-12-23 代码片段

摘要: vuex 总结 时间: 2020-12-23


# APP.vue

<template>
  <div>
    <my-addition></my-addition>

    <p>vuex</p>

    <my-subtraction></my-subtraction>
  </div>
</template>

<script>
import Addition from "./components/Addition.vue";
import Subtraction from "./components/Subtraction.vue";

export default {
  data() {
    return {};
  },
  components: {
    "my-addition": Addition,
    "my-subtraction": Subtraction,
  },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# main.js

import Vue from "vue";
import App from "./App.vue";
import store from "./store";

Vue.config.productionTip = false;

new Vue({
  store,
  render: (h) => h(App),
}).$mount("#app");
1
2
3
4
5
6
7
8
9
10

# store.js

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0,
  },
  // 只有 mutations 中定义的函数,才有权利修改 state 中的数据
  mutations: {
    add(state) {
      // 不要在 mutations 函数中,执行异步操作
      // setTimeout(() => {
      //   state.count++
      // }, 1000)
      state.count++;
    },
    addN(state, step) {
      state.count += step;
    },
    sub(state) {
      state.count--;
    },
    subN(state, step) {
      state.count -= step;
    },
  },
  actions: {
    addAsync(context) {
      setTimeout(() => {
        // 在 actions 中,不能直接修改 state 中的数据;
        // 必须通过 context.commit() 触发某个 mutation 才行
        context.commit("add");
      }, 1000);
    },
    addNAsync(context, step) {
      setTimeout(() => {
        context.commit("addN", step);
      }, 1000);
    },
    subAsync(context) {
      setTimeout(() => {
        context.commit("sub");
      }, 1000);
    },
    subNAsync(context, step) {
      setTimeout(() => {
        context.commit("subN", step);
      }, 1000);
    },
  },
  getters: {
    showNum(state) {
      return "当前最新的数量是【" + state.count + "】";
    },
  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

# Addition.vue

<template>
  <div>
    <!-- <h3>当前最新的count值为:{{$store.state.count}}</h3> -->
    <h3>{{ $store.getters.showNum }}</h3>
    <button @click="btnHandler1">+1</button>
    <button @click="btnHandler2">+N</button>
    <button @click="btnHandler3">+1 Async</button>
    <button @click="btnHandler4">+N Async</button>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    btnHandler1() {
      this.$store.commit("add");
    },
    btnHandler2() {
      // commit 的作用,就是调用 某个 mutation 函数
      this.$store.commit("addN", 3);
    },
    // 异步地让 count 自增 +1
    btnHandler3() {
      // 这里的 dispatch 函数,专门用来触发 action
      this.$store.dispatch("addAsync");
    },
    btnHandler4() {
      this.$store.dispatch("addNAsync", 5);
    },
  },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# Subtraction.vue

<template>
  <div>
    <!-- <h3>当前最新的count值为:{{count}}</h3> -->
    <h3>{{ showNum }}</h3>
    <button @click="btnHandler1">-1</button>
    <button @click="subN(3)">-N</button>
    <button @click="subAsync">-1 Async</button>
    <button @click="subNAsync(5)">-N Async</button>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from "vuex";

export default {
  data() {
    return {};
  },
  computed: {
    // 据映射为计算属性(state)
    ...mapState(["count"]),
    // 映射为计算属性(getter)
    ...mapGetters(["showNum"]),
  },
  methods: {
    // 获得mapMutations映射的sub、subN函数(mutation)
    ...mapMutations(["sub", "subN"]),
    // 获得mapActions映射的addAsync函数(action)
    ...mapActions(["subAsync", "subNAsync"]),
    btnHandler1() {
      // 调用sub函数完成对数据的操作
      this.sub();
    },
  },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 总结

A.state

state 提供唯一的公共数据源,所有共享的数据都要统一放到 store 中的 state 中存储

在组件中访问 state 的方式:

1).this.$store.state.全局数据名称  如:this.$store.state.count

2).先按需导入 mapState 函数: import { mapState } from 'vuex'

然后数据映射为计算属性: computed:{ ...mapState(['全局数据名称']) }
1
2
3
4
5
6
7

B.mutation

mutation 用于修改变更$store 中的数

1).声明
mutations: {
    add(state,step){
      //第一个形参永远都是state也就是$state对象
      //第二个形参是调用add时传递的参数
      state.count+=step;
    }
  }

2).使用:方式一

methods:{
  Add(){
    //使用commit函数调用mutations中的对应函数,
    //第一个参数就是我们要调用的mutations中的函数名
    //第二个参数就是传递给add函数的参数
    this.$store.commit('add',10)
  }
}

3).使用:方式二
import { mapMutations } from 'vuex'

methods:{
  ...mapMutations(['add'])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

C.action

在 mutations 中不能编写异步的代码,会导致 vue 调试器的显示出错。

在 vuex 中使用 action 来执行异步操作。

3).声明
actions: {
  addAsync(context,step){
    setTimeout(()=>{
      context.commit('add',step);
    },2000)
  }
}

2).使用:方式一
<button @click="AddAsync">...+1</button>

methods:{
  AddAsync(){
    this.$store.dispatch('addAsync',5)
  }
}

3).使用:方式二
import { mapActions } from 'vuex'

methods:{
  ...mapMutations(['subAsync'])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

D.getter

getter 用于对 store 中的数据进行加工处理形成新的数据

它只会包装 store 中保存的数据,并不会修改 store 中保存的数据,当 store 中的数据发生变化时,getter 生成的内容也会随之变化


2).声明
export default new Vuex.Store({
  .......
  getters:{
    //添加了一个showNum的属性
    showNum : state =>{
      return '最新的count值为:'+state.count;
    }
  }
})

2).使用:方式一
{{$store.getters.showNum}}

3).使用:方式二
import { mapGetters } from 'vuex'
computed:{
  ...mapGetters(['showNum'])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

E.namespaced

export default {
  // namespaced: true 的方式使其成为带命名空间的模块。保证在变量名一样的时候,添加一个父级名拼接。
  namespaced: true,
  state,
  mutations,
  actions,
};
1
2
3
4
5
6
7

使用模块中的 mutations、getters、actions 时候要加上模块名。

格式:模块名/模块中的 mutations

例如:使用 commint 执行 mutations。(settings.js 模块中 changeSetting 方法)

this.$store.commit('settings/changeSetting',userInfo)

获取 state 属性时加上模块名

格式:store.state.模块名.模块属性

例如:获取 state 属性。

$store.state.userInfo.userName

编辑文档
最后一次更新: 2021/2/2 下午12:34:17
欢迎来到小屋。
看板娘