跳至内容

API 文档文档


API 文档 / pinia / mapActions

函数: mapActions()

允许直接使用存储中的操作,而无需使用组合 API (setup()),通过生成一个要在组件的 methods 字段中展开的对象。

参数

要映射的存储

参数

数组或对象

mapActions(useStore, keyMapper)

mapActions<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapActionsObjectReturn<A, KeyMapper>

允许直接使用存储中的操作,而无需使用组合 API (setup()),通过生成一个要在组件的 methods 字段中展开的对象。该对象的键值对是操作,而键是生成的函数的名称。

类型参数

Id extends string

S extends StateTree

G extends _GettersTree<S>

A

KeyMapper extends Record<string, keyof A>

参数

useStore: StoreDefinition<Id, S, G, A>

要映射的存储

keyMapper: KeyMapper

用于定义操作新名称的对象

返回值

_MapActionsObjectReturn<A, KeyMapper>

示例

js
export default {
  methods: {
    // other methods properties
    // useCounterStore has two actions named `increment` and `setCount`
    ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
  },

  created() {
    this.moar()
    this.setIt(2)
  }
}

mapActions(useStore, keys)

mapActions<Id, S, G, A>(useStore, keys): _MapActionsReturn<A>

允许直接使用存储中的操作,而无需使用组合 API (setup()),通过生成一个要在组件的 methods 字段中展开的对象。

类型参数

Id extends string

S extends StateTree

G extends _GettersTree<S>

A

参数

useStore: StoreDefinition<Id, S, G, A>

要映射的存储

keys: keyof A[]

要映射的操作名称数组

返回值

_MapActionsReturn<A>

示例

js
export default {
  methods: {
    // other methods properties
    ...mapActions(useCounterStore, ['increment', 'setCount'])
  },

  created() {
    this.increment()
    this.setCount(2) // pass arguments as usual
  }
}