vuex的使用之mapActions

vue项目中,经常会使用到vuex,vuex是vue的一个状态管理。

本文简单总结一下:vuex中mapActions的使用。

如果一个方法或多个方法需要在多个页面和组件中使用,那么,可以使用mapActions。

一.vuex中声明变量个方法

1.在store/module/user.js中接口引入

import { login, logout, getUserInfo } from '@/api/user'

2.在mutations中书写方法:


actions: {
 // 登录
    handleLogin ({ commit }, {userName, password}) {
      userName = userName.trim()
      return new Promise((resolve, reject) => {
        login({
          userName,
          password
        }).then(res => {
          const data = res.data
          commit('setToken', data.token)
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('setToken', '')
          commit('setAccess', [])
          resolve()
        }).catch(err => {
          reject(err)
        })
        // 如果你的退出登录无需请求接口,则可以直接使用下面三行代码而无需使用logout调用接口
        // commit('setToken', '')
        // commit('setAccess', [])
        // resolve()
      })
    },
    // 获取用户信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo(state.token).then(res => {
            const data = res.data
            commit('setAvator', data.avator)
            commit('setUserName', data.name)
            commit('setUserId', data.user_id)
            commit('setAccess', data.access)
            commit('setHasGetInfo', true)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    }
  }
 }
 

二.在各页面中使用

1,引入

  import { mapActions } from 'vuex'

2,在method里

  ...mapActions([
      'handleLogOut'
    ]),

或多个方法

...mapActions([
      'handleLogin',
      'getUserInfo'
    ]),

3,当前页面方法中调用

  handleSubmit ({ userName, password }) {
      this.handleLogin({ userName, password }).then(res => {
        this.getUserInfo().then(res => {
          this.$router.push({
            name: this.$config.homeName
          })
        })
      })
    }
  • 14
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vuex mapActions 使用教程: 1. 在组件中引入 Vuex: ``` import Vuex from 'vuex' Vue.use(Vuex) ``` 2. 创建存储实例: ``` const store = new Vuex.Store({ state: { ... }, mutations: { ... }, actions: { ... } }) ``` 3. 使用 mapActions 辅助函数: ``` import { mapActions } from 'vuex' ``` 4. 在组件中使用 mapActions: ``` export default { // ... methods: { ...mapActions(['action1', 'action2']), // 或者 ...mapActions({ a1: 'action1', a2: 'action2' }) } } ``` 5. 调用存储中的动作: ``` this.action1() this.a2() ``` 6. 如果需要传递参数,可以在调用动作时传递: ``` this.action1(payload) this.a2({ param1, param2 }) ``` ### 回答2: VuexVue.js 官方的状态管理库,它可以让我们更好地管理组件状态的包括响应式数据、计算属性、触发方法等。Vuex 提供了一批辅助函数来简化对 store 的访问,其中一个常用的辅助函数是 mapActions,它可以将 store 中的 action 映射到组件的方法中,方便我们调用。 使用 mapActions,需要首先在 Vue 组件中引入它: ```javascript import { mapActions } from 'vuex' ``` 然后,在 Vue 组件的 computed 属性中定义映射方法: ```javascript computed: { ...mapActions([ 'increment', 'decrement', ]) } ``` 这个例子中,我们使用了 spread (展开)操作符来将 mapActions 返回的对象合并到 computed 对象中。注意,我们传递的数组里面的字符串是 action 的名称,即 store 中定义的方法。 最后,在我们的组件方法中,可以使用映射方法调用 action: ```javascript methods: { ...mapActions([ 'increment', 'decrement', ]), add() { this.increment(); }, reduce() { this.decrement(); } } ``` 这个例子中,我们定义了 add 和 reduce 方法,并在方法中通过 this.increment() 和 this.decrement() 调用了映射action。 总的来说,VuexmapActions 函数让我们不需要在组件中手动调用 $store.dispatch 方法,而是把 action 作为一个映射函数传递进去,使组件可以直接调用 store 中的方法,显著简化了组件的代码量,提高了开发效率。 ### 回答3: VuexVue.js应用程序开发的状态管理模式,提供集中式存储管理,方便管理Vue应用的所有组件。Vuex中提供了一些方法,如mapActions(),可以将actions映射到组件的method中,以便简化组件中的代码。下面是Vuex mapActions的使用教程。 使用方法: 1.导入Vuex库和需要操作的actions。 ``` import { mapActions } from "vuex"; import { NAME_OF_ACTIONS } from "../vuex/actions.js"; ``` 2.定义好组件的methods。 ``` methods: { method1() {}, method2() {}, } ``` 3.使用mapActions方法映射actions到组件的methods中。 ``` methods: { ...mapActions({ action1: NAME_OF_ACTIONS.ACTION1, action2: NAME_OF_ACTIONS.ACTION2, }), method1() {}, method2() {}, } ``` 4.在组件中使用定义好的方法。 ``` <button @click="action1()">执行action1</button> <button @click="action2()">执行action2</button> ``` 以上就是Vuex mapActions的使用教程,这个方法可以让我们更方便地获取Vuex中的actions,提高代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值