在Vuex中不使用操作就提交突变是否不好

在Vuex中不使用操作就提交突变是否不好

本文介绍了在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.

我认为,因为它已在文档中使用,所以它必须是可以接受的模式,并且我想知道是否跳过动作并直接触发Mutations是否是任何其他状态管理库(如Flux的Redux本身)认可的模式.

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中不使用操作就提交突变是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!