Ashun's 技術駅 Ashun's 技術駅
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ashun

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 01mvvm和mvc
  • 02安装和使用
  • 03模版语法
  • 04生命周期
  • 05指令
  • 06方法-计算属性-监听器
  • 07class和style绑定
  • 08条件渲染
  • 09列表渲染
  • 10事件处理
  • 11表单输入绑定
  • 12组件
  • 13组件之间传递数据
  • 14props验证
  • 15插槽
  • 16动态组件
  • 17处理边界情况
  • 18脚手架
  • 19Vuex
    • 1、安装&使用
    • 2、state 数据状态
    • 3、getters 计算属性
    • 4、mutations 提交
    • 5、actions 异步提交
    • 6、modules 模块化
  • 20VueRouter
  • 21自定义指令
  • 22渲染函数
  • 23插件
  • 24VueConfig
  • 《Vue》
xugaoyi
2022-02-14
目录

19Vuex

# Vuex

为vue定制的数据状态管理模式。

# 1、安装&使用

npm i vuex -S
1
export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    }
})
1
2
3
4
5

# 2、state 数据状态

定义

{{this.$store.state.num}}
1

调用

<template>
  <div>
      <p>{{this.$store.state.num}}</p>
  </div>
</template>
1
2
3
4
5

计算属性

<template>
  <div>
      {{num}}
  </div>
</template>

<script>
export default {
    computed:{
        num(){
            return this.$store.state.num
        }
    }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

辅助函数

<template>
  <div>
      {{num}}
  </div>
</template>

<script>
import {mapState} from "vuex"
export default {
    computed:{
        ...mapState(["num"])
    }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 3、getters 计算属性

定义

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        msg:"hello world",
    },
    getters:{
        toReverse(state){//反转
            return state.msg.split("").reverse().join("")
        }
    }
})
1
2
3
4
5
6
7
8
9
10

使用

<template>
  <div>
      {{this.$store.getters.toReverse}}
  </div>
</template>
1
2
3
4
5

以其他getters作为参数

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        msg:"hello world",
    },
    getters:{
        toReverse(state){//反转
            return state.msg.split("").reverse().join("")
        },
        toUpperCase(state,getters){//大写
            return getters.toReverse.toUpperCase()
        }
    }
})
1
2
3
4
5
6
7
8
9
10
11
12
13

通过方法访问

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        arr:[{id:1,name:"小明"},{id:2,name:"小红"},{id:3,name:"小黄"}]
    },
    getters:{
        filter(state){
            return (id)=>{
                return state.arr.filter((item)=>{
                    return item.id === id
                })
            }
        }
    }
})

{{this.$store.getters.filter(2)}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

辅助函数

<template>
  <div>
      {{toFilter(18)}}
  </div>
</template>

<script>
import {mapGetters} from "vuex"
export default {
    computed:{
        ...mapGetters(["toFilter"])
    }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 4、mutations 提交

vuex的事件,用于更改在state中定义的数据

定义

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交
        add(state){
            state.num++
        }
    }
})
1
2
3
4
5
6
7
8
9
10

访问

<template>
  <div id="app">
    <button @click='add'>按钮</button>
    {{this.$store.state.num}}
  </div>
</template>

<script>
export default {
  name: 'App',
  methods:{
    add(){
      this.$store.commit("add")
    }
  }
}
</script>

<style>

</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

提交载荷(添加参数)

import Vue from "vue"
import Vuex from "vuex"//vuex插件

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交
        add(state,payload){
            state.num+=payload
        }
    }
})

methods:{
    add(){
      this.$store.commit("add",2)
    }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

使用常量替代 Mutation 事件类型

import Vue from "vue"
import Vuex from "vuex"//vuex插件
import {ADD} from "./mutations-type.js"

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交
        [ADD](state,payload){
            state.num+=payload
        }
    }
})

export const ADD = "ADD"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

辅助函数

<template>
  <div>
    <button @click="ADD">添加</button>
  </div>
</template>

<script>
import {mapMutations} from "vuex"
export default {
  methods: {
    ...mapMutations(["ADD"])
  },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 5、actions 异步提交

vuex的异步事件,用于异步的更改在state中定义的数据。

定义

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交(只有这里能修改数据)
        add(state){
            state.num++
        }
    },
    actions:{//异步提交(只有这里能写异步方法)
        aysncAdd(context){
            context.commit("add")
        }
    }
})

add(){
  this.$store.dispatch("aysncAdd")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

简写

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交(只有这里能修改数据)
        add(state){
            state.num++
        }
    },
    actions:{//异步提交(只有这里能写异步方法)
        aysncAdd({commit}){
            commit("add")
        }
    }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

dispatch访问其他actions

import Vue from "vue"
import Vuex from "vuex"//vuex插件

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交(只有这里能修改数据)
        add(state){
            state.num++
        }
    },
    actions:{//异步提交(只有这里能写异步方法)
        aysncAdd({commit}){
            commit("add")
        },
        asyncAdd2({dispatch}){
            dispatch("aysncAdd")
        }
    }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

提交载荷

import Vue from "vue"
import Vuex from "vuex"//vuex插件

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交(只有这里能修改数据)
        add(state,payload){
            state.num+=payload 
        }
    },
    actions:{//异步提交(只有这里能写异步方法)
        aysncAdd({commit},payload){
            commit("add",payload)
        },
        asyncAdd2({dispatch},payload){
            dispatch("aysncAdd",payload)
        }
    }
})

this.$store.dispatch("asyncAdd2",2)//派发
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

辅助函数

<template>
  <div id="app">
    <button @click="asyncAdd2(2)">按钮</button>
    {{this.$store.state.num}}
  </div>
</template>

<script>
import { mapActions } from "vuex"
export default {
  name: 'App',
  methods:{
    ...mapActions(["asyncAdd2"])
  }
}
</script>

<style>

</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

异步案例

promise

import Vue from "vue"
import Vuex from "vuex"//vuex插件

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    state:{//数据状态(类似于data数据)
        num:1
    },
    mutations:{//同步提交(只有这里能修改数据)
        add(state){
            state.num++ 
        },
        add2(state){
            state.num+=2
        }
    },
    actions:{//异步提交(只有这里能写异步方法)
        asyncAdd({commit}){
            return new Promise((resolve)=>{
                setTimeout(()=>{
                    commit("add")
                    resolve()
                },2000)
            })
        },
        asyncAdd2({commit}){
            return new Promise((resolve)=>{
                commit("add2")
                resolve()
            })
        }
    }
})

add(){
  this.$store.dispatch("asyncAdd").then(()=>{
    this.$store.dispatch("asyncAdd2")
  })
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

async await

async add(){
  await this.$store.dispatch("asyncAdd")
  await this.$store.dispatch("asyncAdd2")
}
1
2
3
4

# 6、modules 模块化

把store那功能模块分割

定义

store/index.js

import Vue from "vue"
import Vuex from "vuex"//vuex插件
import {acom} from "./modules/acom.js"
import {bcom} from "./modules/bcom.js"

Vue.use(Vuex)//使用插件

export default new Vuex.Store({
    modules:{
        acom,
        bcom
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

store/modules/acom.js

export const acom = {
    namespaced:true,//命名空间
    state:{
        num:1
    },
    mutations:{
        add(state){
            state.num++
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11

访问

//获取state
{{this.$store.state.acom.num}}

//执行getters
{{this.$store.getters["acom/add"]}}

//执行mutations
this.$store.commit("acom/add")

//执行actions
this.$store.dispatch("acom/asyncAdd")
1
2
3
4
5
6
7
8
9
10
11

辅助函数

<template>
  <div>
    <div>组件a</div>
    <div>{{ num }}</div>
    <button @click="add">按钮</button>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations } = createNamespacedHelpers("acom");
export default {
  computed: {
    ...mapState(["num"]),
  },
  methods: {
    ...mapMutations(["add"]),
  },
};
</script>

<style></style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41
18脚手架
20VueRouter

← 18脚手架 20VueRouter→

最近更新
01
课件-react路由-V6
01-22
02
课件-国际化
01-22
03
课件-redux-toolkit
01-22
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式