vuex详细使用(总结)

vuex各个参数

  • state 存放状态
  • mutations state成员操作
  • getters 加工state成员给外界
  • actions 异步操作
  • modules 模块化状态管理

VueX的工作流程
vuex工作流程图片

  • state 使用:

HTML中

<h1>{{ $store.state.name }}</h1>

js中

console.log(this.$store.state.name)
  • mutations 使用:

mutations是操作state数据的方法的集合,比如对该数据的修改、增加、删除等等。state无法直接操作,只能用mutations操作
单个值提交时:

this.$store.commit('edit',15)

当需要多参提交时,推荐把他们放在一个对象中来提交:

this.$store.commit('edit',{age:15,sex:'男'})

接收挂载的参数:

edit(state,payload){
    state.name = 'jack'
    console.log(payload) // 15或{age:15,sex:'男'}
}

另一种提交方式

this.$store.commit({
    type:'edit',
    payload:{
        age:15,
        sex:'男'
    }
})

Vue.set 为某个对象设置成员的值,若不存在则新增

Vue.set(state,"age",15)

Vue.delete 删除成员

Vue.delete(state,'age')
  • getters 使用:

可以对state中的成员加工后传递给外界

Getters中的方法有两个默认参数:
state 当前VueX对象中的状态对象
getters 当前getters对象,用于将getters下的其他getter拿来用

getters:{
    nameInfo(state){
        return "姓名:"+state.name
    },
    fullInfo(state,getters){
        return getters.nameInfo+'年龄:'+state.age
    }  
}

组件中调用:

this.$store.getters.fullInfo
  • actions 使用

由于直接在mutation方法中进行异步操作,将会引起数据失效。所以提供了Actions来专门进行异步操作,最终提交mutation方法。

Actions中的方法有两个默认参数:
context 上下文(相当于箭头函数中的this)对象
payload 挂载参数

actions:{
    aEdit(context,payload){
        setTimeout(()=>{
            context.commit('edit',payload)
        },2000)
    }
}

组件中调用:

this.$store.dispatch('aEdit',{age:15})
  • Models 使用
    当项目庞大,状态非常多时,可以采用模块化管理模式。Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割。
models:{
    a:{
        state:{},
        getters:{},
        ....
    }
}

组件内调用模块a的状态:

this.$store.state.a

而提交或者dispatch某个方法和以前一样,会自动执行所有模块内的对应type的方法:

this.$store.commit('editKey')
this.$store.dispatch('aEditKey')

修改至:怪兽难吃素

文档:vuex

评价:我觉得作者"怪兽难吃素",总结的比官网好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和介绍了基于Vue实现发布订阅模式的方式,即使用事件总线(EventBus)来进行组件之间的通信。首先,在一个模块中创建事件总线,并将其导出,以便其他模块可以使用或监听它。然后,在需要订阅事件的地方,使用`eventBus.$on`方法来监听事件,指定事件名称和回调函数。当事件被触发时,回调函数将会被执行。在需要发布事件的地方,使用`eventBus.$emit`方法来触发事件,指定事件名称和传递的参数。 关于Vuex使用举例,Vuex是Vue的官方状态管理库,它提供了一种集中式存储管理应用的方式。和事件总线类似,Vuex也可以用于组件之间的通信。不过,Vuex更适合用于管理应用的共享状态和数据。 举例如下,假设有一个应用需要管理一个名为`count`的状态,可以使用Vuex来实现: - 首先,在创建Vue实例之前,引入并使用Vuex插件。可以在`main.js`文件中进行如下配置: ``` import Vue from 'vue' import Vuex from 'vuex' import App from './App.vue' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state, payload) { state.count += payload.num } } }) new Vue({ store, render: h => h(App) }).$mount('#app') ``` - 然后,在需要使用`count`状态的组件中,可以使用`mapState`和`mapMutations`辅助函数来简化代码。例如,在`showNum.vue`组件中: ```vue <template> <div> 计算和: {{ count }} <button @click="incrementCount">增加</button> </div> </template> <script> import { mapState, mapMutations } from 'vuex' export default { computed: { ...mapState(['count']) }, methods: { ...mapMutations(['incrementCount']) } } </script> ``` - 最后,在组件中使用`count`状态和`incrementCount`方法即可。`count`可以通过计算属性的方式获取,`incrementCount`可以通过方法来触发对应的mutation。 总结来说,事件总线(EventBus)适合用于简单的组件之间通信,而Vuex适合用于管理应用的共享状态和数据。在具体的应用场景中,可以根据需要选择适合的方式来进行组件之间的通信和状态管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vuex使用详细说明及实例](https://blog.csdn.net/to_the_Future/article/details/122809337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue中组件之间8种通信方式](https://blog.csdn.net/liushihao_/article/details/119617378)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值