Vuex辅助函数

辅助函数

mapState 辅助函数

当一个组件需要获取多个状态的时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键:

// 在单独构建的版本中辅助函数为 Vuex.mapState
import { mapState } from 'vuex'

export default {
  // ...
  computed: mapState({
    // 箭头函数可使代码更简练
    count: state => state.count,

    // 传字符串参数 'count' 等同于 `state => state.count`
    countAlias: 'count',

    // 为了能够使用 `this` 获取局部状态,必须使用常规函数
    countPlusLocalState (state) {
      return state.count + this.localCount
    }
  })
}

当映射的计算属性的名称与 state 的子节点名称相同时,我们也可以给 mapState 传一个字符串数组。

computed: mapState([
  // 映射 this.count 为 store.state.count
  'count'
])

对象展开运算符

mapState 函数返回的是一个对象。我们如何将它与局部计算属性混合使用呢?通常,我们需要使用一个工具函数将多个对象合并为一个,以使我们可以将最终对象传给 computed 属性。但是自从有了对象展开运算符(opens new window),我们可以极大地简化写法:

computed: {
  localComputed () { /* ... */ },
  // 使用对象展开运算符将此对象混入到外部对象中
  ...mapState({
    // ...
  })
}

mapGetters 辅助函数

mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性:

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用对象展开运算符将 getter 混入 computed 对象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}

如果你想将一个 getter 属性另取一个名字,使用对象形式:

...mapGetters({
  //`this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

mapMutations 辅助函数

你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations([
      'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`

      // `mapMutations` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
    })
  }
}

mapActions 辅助函数

你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):

import { mapActions } from 'vuex'

export default {
  // ...
  methods: {
    ...mapActions([
      'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`

      // `mapActions` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
    })
  }
}

modules

state

this.$store.state.moduleA.usernam // 在调用的时候加上了模块名称

mutations

this.$store.commit('uptUserName') // 在调用的时候 没加 对应的模块名称

命名空间

namesapced: true 代表开启

声明

image-20220122224825957

使用

image-20220122225515696

使用modules之后获取里面的属性或方法

state里面的属性 ==> this.$store.state + '模块名称' + state里面的属性名

mutations 里面的方法==> this.$store.commit('模块名称/mutations 里的方法名')

actions里面的方法==> this.$store.dispatch('模块名称/actions里的方法名')

getters里面的属性==> this.$store.getters['模块名称/getters里的属性名']

使用辅助函数获取属性或方法

mapSate

函数形式

  computed: {
    ...mapState({
      mainMenu: state => state.permission.addRouters
    })
  }
mapGetters

在获取的模块方法名称前面加上模块名称

  computed: {
    ...mapGetters({
      mainMenu: 'permission/addRouters'
    })
  }
mapMutations

在获取的模块方法名称前面加上模块名称

  methods: {
    ...mapMutations({
      mainMenu: 'permission/addRouters'
    })
  }
mapActions

在获取的模块方法名称前面加上模块名称

  methods: {
    ...mapActions({
      mainMenu: 'permission/addRouters'
    })
  }

Vuex缺陷

当刷新页面之后 存储在vuex state当中的数据就重新初始化

如果想要在vuex长久的存储数据 需要vuex与本地存储结合使用

  1. 既把数据存储到vuex当中, 也把数据存储到本地
  2. 当刷新页面 vuex会重新初始化 state里面的数据也会重新初始化 初始化的时候优先使用本地存储的数据
    username: localStorage.getItem('token') ? JSON.parse(localStorage.getItem('token')) : ''
posted @   Dexlux  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示