Vuex基本用法

 一、Vuex基本写法

store文件夹下,新建index.js

import Vue from 'vue'
import Vues from 'vuex'

Vue.use(Vuex)

const modulesFiles = require.context('./modules', true, /\.js$/)

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.store({
    modules
})

export default store

main.js中引入store/index.js

import store from './store/index'


new Vue({
  router,
  store,
  render: (h) => h(App)
}).$mount('#app')

新建modules 文件夹,新建 home.js


const state = {
  menuList:[],
  selectedMenu: 1, 
  
}

const getters = {
  totalSum: (state) => {
    return state.selectedMenu + 1
  }
}

const mutations = {
  updateMenuList: (state, data) => {
    state.menuList= data
  },
 
}
const actions = {
  // -- 获取 菜单 list
  async getMenuList({ dispatch, state, commit }, paramObj = null) {
    const data = await getBubbleShowData(paramObj)
    commit('updateMenuList', data)
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

二、Vuex中四个map方法的使用 mapState mapGetters mapActions mapMutations

1. mapState: 帮助我们映射state中的数据为计算属性,

  this.$store.state.home.checkedChildTheme // home是store的module

import {mapState,mapGetters} from 'vuex'

computed: {
    //借助mapState生成计算属性:selectMenu(对象写法)
     ...mapState({selectMenu:'selectMenu'}),
    
    // 获取modules里的state
    ...mapState({
      selectMenu: (state) => state.home.selectMenu
    })

         
    //借助mapState生成计算属性:selectMenu(数组写法)
    ...mapState(['selectMenu']),

    // 获取modules里的state
    ...mapState('home', ['selectMenu'])

}

2.mapGetter帮助我们映射getters中的数据为计算属性: 

this.$store.getters.totalSum

import {mapState,mapGetters} from 'vuex'

computed: {
    //借助mapGetters生成计算属性:totalSum(对象写法)
    ...mapGetters({totalSum:'totalSum'}),

    //借助mapGetters生成计算属性:totalSum(数组写法)
    ...mapGetters(['totalSum'])
},
// 原生写法

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

3.mapActions用来生成actions对话,包含this.$store.dispatch(xxx)的函数

this.$store.dispatch('home/getMenuList', {})

import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

methods:{
    //靠mapActions生成:getList(对象形式)
    ...mapActions({getList:'getList'})

    //靠mapActions生成:getList(数组形式)
    ...mapActions(['getList'])

    // this.getList() 
}

4.mapMutations帮助我们生成mutations中的方法, 即 this.$store.commit()函数

 this.$store.commit('home/updateMenuList', val) 

methods:{
    //靠mapActions生成:updateSelectedMenu(对象形式)
    ...mapActions({updateSelectedMenu:'updateSelectedMenu'})

    //靠mapActions生成:updateSelectedMenu(数组形式)
    ...mapActions(['updateSelectedMenu'])
}

Vue3是目前最新的Vue.js版本,相比Vue2有很多新特性和更好的性能。而Vuex是Vue.js官方推荐的状态管理工具,用于处理复杂的应用程序中的共享状态。 下面是Vue3和Vuex基本使用方法: 1. 在项目中安装Vue3和Vuex: ```bash npm install vue@next vuex@next --save ``` 2. 创建一个store.js文件用于配置Vuex: ```javascript import { createStore } from 'vuex' const store = createStore({ state: { count: 0 }, mutations: { increment(state) { state.count++ } }, actions: { increment(context) { context.commit('increment') } }, getters: { getCount(state) { return state.count } } }) export default store ``` 这里我们定义了一个state对象来存储应用程序的状态,一个mutation函数来改变状态,一个action函数来分发mutation,一个getter函数来获取状态。 3. 在main.js中引入store: ```javascript import { createApp } from 'vue' import App from './App.vue' import store from './store' createApp(App).use(store).mount('#app') ``` 4. 在组件中使用Vuex: ```html <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { computed: { ...mapGetters(['getCount']), count() { return this.getCount } }, methods: { ...mapActions(['increment']) } } </script> ``` 这里我们使用了Vuex提供的mapGetters和mapActions函数来映射state中的count属性和increment函数到组件中。 以上就是Vue3和Vuex基本使用方法,可以根据自己的需求进行扩展和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值