网站首页 > 技术文章 正文
刚开始接触Vuex,总是会把其中的概念搞混淆,然后弄得一头雾水,这次总结记录一下,为下次节省时间
1.state 公共数据源
使用方法:
第一种方式
this.$stroe.state.全局数据名称(页面使用不用加this)
第二种方式
1.从vuex中按需导入 mapstate 函数
import { mapstate } from 'vuex'
2.通过刚才道德mapState函数,将当前组件需要的全局数据,映射为当前组件的computed计算属性
computed:{
...mapState(['count'])
}
2. Mutation 用于变更Store中的数据
2.1 只能通过mutation 用于变更Store 中的数据,不可以直接操作 Store 中的数据
2.2 通过这种方式虽然操作起来稍微繁琐一点,但是可以集中监控所有数据的变化。
// 定义 Mutation
const store = new Vuex.Store({
state: {
count:0
},
mutations:{
add(state){
//变更状态
state.count++
},
addSetTimeout({
//不要在mutations中执行异步操作
setTimeout(()=>{
state.count++
},1000)
})
}
})
// 在vue或者组件中使用 Mutation 第一种方式
this.$store.commit('add')
// 在vue或者组件中使用 Mutation 第二种方式
import { mapMutations } from 'vuex'
export default {
methods:{
...mapMutations(['add'])
btnHandlerl(){
this.add()
}
}
}
3. Action 专门用来处理异步任务
如果通过异步操作变更数据,必须通过Action,而不是使用Mutations,但是在Action中还是要通过触发Mutation的方式简介变更数据。
// 定义Action
const store - new Vuex.Store({
// ...省略其他代码
mutations:{
add(state,step/*step代表可以增加传入的参数*/){
//变更状态
state.count++
}
},
actions:{
addAsync(context){
setTimeout(()=>{
// 在Action 中,不能直接修改 state 中的数据,
// 必须通过 context.commit()触发某个 mutation 才行
context.commit('add')
},1000)
}
}
})
// Action使用
//这里的 dispatch 函数 专门用来触发 action
this.$store.dispatch('addAsync')
3.1 触发actions 异步任务时携带参数:
// 定义Action
const store - new Vuex.Store({
// ...省略其他代码
mutations:{
addN(state, step){
//变更状态
state.count += step
}
},
actions:{
addNAsync(context, step){
setTimeout(()=>{
// 在Action 中,不能直接修改 state 中的数据,
// 必须通过 context.commit()触发某个 mutation 才行
context.commit('addN', step)
},1000)
}
}
})
// Action使用
//这里的 dispatch 函数 专门用来触发 action
this.$store.dispatch('addNAsync', 5)
3.2 触发actions 的第二种方式
// 从 Vuex中按需导入 mapActions 函数
import { mapActions } from 'vue'
通过刚才导入的mapActions函数, 映射为当前组件的 methods 方法
// 将指定的 actions 函数,映射为 当前组件的 methods 函数
methods: {
...mapActions(['addAsync', 'addNAsync']),
BTNHandler3(){
this.subAsync()
}
}
如果页面使用actions,在通过导入的mapActions函数映射为当前组件的 methods 方法,然后在页面直接使用
methods: {
...mapActions(['addAsync', 'addNAsync'])
}
<button @click="addAsync">addAsync</button>
<button @click="addNAsync">addNAsync</button>
4. Getter 用于对 Store 中的数据进行加工处理形成的数据
4.1 Getter 可以对 Store 中已有的数据加工处理之后形成新的数据, 类似Vue 的计算属性
4.2 Store 中数据发生变化, Getter 的数据也会跟着发生变化。
// 定义 Getter
const store = new Vuex.Store({
state:{
count: 0
},
getters:{
showNum: state =>{
return '当前最新的数量是【' + state.count + '】'
}
}
})
使用 getters 的第一种方式
this.$store.getters.showNum
以上就是vuex的使用方法与总结
猜你喜欢
- 2024-09-29 Vue实战——vue+router+vuex导航守卫进行身份验证
- 2024-09-29 shopping开源项目用vue+vue-router+vuex实现电商网站基本功能
- 2024-09-29 Vuex状态管理(vuex状态管理几种状态)
- 2024-09-29 vue2视频教程系列第二十七节—vuex中getters和actions的使用
- 2024-09-29 实现vuex(实现人生价值的根本途径是)
- 2024-09-29 Vuex 学习笔记(vuex视频教学)
- 2024-09-29 Vue 3学习:4. 集成vuex(vue集成js)
- 2024-09-29 Vue组件间通信(vue组件间通信的方法)
- 2024-09-29 vue-admin-template调用action获取用户资料
- 2024-09-29 vuex的实现以及数据流向(vuex单向数据流图)
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 505℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 485℃MySQL service启动脚本浅析(r12笔记第59天)
- 465℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 462℃启用MySQL查询缓存(mysql8.0查询缓存)
- 443℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 422℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 418℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)