当前位置:首页 > 开发教程 > js/jQuery教程 >

vuex如何在非组件中调用mutations办法

时间:2022-03-28 13:54 来源:未知 作者:谢谢你给的幸福 收藏

这篇文章主要介绍了vuex如何在非组件中调用mutations方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

在非组件中调用mutations方法

一般情况下调用 mutations.js 中的方法都是在组件中,如果想在非组件中调用,则需要使用如下方式

在组件中调用

1
2
3
4
5
6
7
8
9
10
11
12
import {mapMutations} from 'vuex'
import {SET_IS_LOGIN} from 'store/mutation-types'
export default {
? ? methods: {
? ? ? ? ...mapMutations({
? ? ? ? ? ? set_is_login: SET_IS_LOGIN
? ? ? ? }),
? ? ? ? init() {
? ? ? ? ? ? this.set_is_login(true)
? ? ? ? }
? ? }
}

在非组件中调用

1
2
3
4
5
import store from 'store'
import {SET_IS_LOGIN} from 'store/mutation-types'
function init() {
? ? store.commit(SET_IS_LOGIN, true)
}

vuex的mutations属性

mutations属性介绍

是唯一一种方式来修改state中的状态的;在组件的自定义方法中,使用this.$store.commit(‘对应mutations中的方法’, 新的值)方法,把新的值提交给mutations中相对应的方法,mutations属性中的每个方法中有两个参数,分比为state和payload;state其实就是vuex中的state属性,payload叫做mutations的载荷,其实就是传过来的值。一般payload传的是一个对象,这样可以包含多个字段并且记录的 mutation 会更易读:

1
2
3
4
5
mutations: {
? increment (state, payload) {
? ? state.count += payload.amount
? }
}
1
2
3
store.commit('increment', {
? amount: 10
})

对象风格的提交方式

提交 mutation 的另一种方式是直接使用包含 type 属性的对象:

1
2
3
4
store.commit({
? type: 'increment',
? amount: 10
})

当使用对象风格的提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变:

1
2
3
4
5
mutations: {
? increment (state, payload) {
? ? state.count += payload.amount
? }
}

使用常量替代 Mutation 事件类型

使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个 app 包含的 mutation 一目了然:

1
2
// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
1
2
3
// store.js
import Vuex from 'vuex'
import { SOME_MUTATION } from './mutation-types'
1
2
3
4
5
6
7
8
9
const store = new Vuex.Store({
? state: { ... },
? mutations: {
? ? // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
? ? [SOME_MUTATION] (state) {
? ? ? // mutate state
? ? }
? }
})

用不用常量取决于你——在需要多人协作的大型项目中,这会很有帮助。但如果你不喜欢,你完全可以不这样做。

Mutation 必须是同步函数

一条重要的原则就是要记住 mutation 必须是同步函数。为什么?请参考下面的例子:

1
2
3
4
5
6
7
mutations: {
? someMutation (state) {
? ? api.callAsyncMethod(() => {
? ? ? state.count++
? ? })
? }
}

现在想象,我们正在 debug 一个 app 并且观察 devtool 中的 mutation 日志。每一条 mutation 被记录,devtools 都需要捕捉到前一状态和后一状态的快照。然而,在上面的例子中 mutation 中的异步函数中的回调让这不可能完成:因为当 mutation 触发的时候,回调函数还没有被调用,devtools 不知道什么时候回调函数实际上被调用——实质上任何在回调函数中进行的状态的改变都是不可追踪的。

在组件中提交 Mutation

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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')`
? ? })
? }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持源码搜藏网。


下一篇:没有了

js/jQuery教程阅读排行

最新文章