在 Vuex 中不使用操作就提交突变是不是很糟糕? [英] Is it bad to commit mutations without using actions in Vuex?

查看:25
本文介绍了在 Vuex 中不使用操作就提交突变是不是很糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Vuex 有一段时间了,我一直遵循这样的模式:组件使用 Actions 提交 Mutations 来改变 Store.考虑到文档中的这张图,我认为这是正确的做法:

I have been using Vuex for awhile now, and I have always been following the pattern: Components use Actions to commit Mutations to mutate the Store. I thought this was the proper way to do things considering this diagram from the docs:

我遇到过这样的代码:人们直接在组件中提交变更,甚至没有创建除了触发变更之外没有其他目的的简单操作.我什至在 Vuex 文档中找到了几个这样的例子.

I came across code where people were committing mutations directly in components, and not even creating simple actions which have no purpose other than to trigger mutations. I even found several examples of this in the Vuex docs.

我想,因为它在文档中被使用,所以这一定是一种可接受的模式,我想知道跳过 Actions 并直接触发 Mutations 是否是任何其他状态管理库(如 Redux of Flux 本身)认可的模式.

I figured since it's used in the docs this must be an acceptable pattern, and I was wondering if skipping Actions and directly triggering Mutations was a pattern endorsed by any other state management libraries such as Redux of Flux itself.

TLDR:在 Vuex 中直接提交突变是否可以,如果是,其他状态管理库如 Redux 是否使用这样的模式?如果是,他们为什么不呢?

推荐答案

请记住,突变必须同步.如果您(和您的团队)确定不会出现异步内容,您可以直接在组件中提交.换句话说,通过简单直接的操作使用它.

Just keep in mind that mutations have to be synchronous. You can commit directly in components if you (and your team) are sure is there no chance of appearing of something async. In other words, use it with simple and direct operations.

仅在操作中提交作为一项规则可为应用程序代码增加一些清晰度和可靠性.

Committing only in actions as a rule adds some clarity and reliability to application's code.

我没有用过 Redux,但据我所知,一些状态管理器有中间件.使用突变和操作(Vuex 方式)可能会导致大型应用程序难以维护.

I didn't used Redux, but as far as I know, some state managers have middleware. Using mutations and action (Vuex-way) may cause a difficult maintenance into large applications.

在 Vuex 的未来版本中,变更和操作应该是

In the future version of Vuex, mutations and actions should be merged into the one entity.

这篇关于在 Vuex 中不使用操作就提交突变是不是很糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆