Vuex getters是Vue计算的,因为Vuex状态是Vue数据.
场景1
In an action or getter, if you use a list of products multiple times to find a result would you then use getters.products or state.products?
我不太明白你的情况;代码示例可以更好地说明您的意思.
假设您有产品状态,这是一个产品对象数组.如果您需要访问多个地方的已排序(例如)产品,那么生成sortedProducts getter会比每次排序产品更好,因为Vue将缓存结果并且仅在产品数组更改时重新计算其值.
Also, if you needed to use the products 10 times in the same function, would you call getters.products or state.products 10 times or would you assign the products to a variable at the beginning and then use that 10 times? Are there any perfomance gain in any over the others?
如果您关注性能,则无需在函数开头将其分配给变量.访问商店状态或获取者的性能成本可以忽略不计.代码可读性在这里更重要.
情景2
sortedProducts getter函数不返回函数,因此Vuex将缓存结果.
Would it be better to have a state instead that is manually updated by an action and mutation?
如果你在谈论你的sortedProducts getter,没有.
Generally, would it ever make sense to have getters returning functions that has to do with large amount of data?
您需要getter返回函数的唯一情况是,您希望getter能够接受参数,在这种情况下,getter更像是Vue组件方法而不是Vue组件计算属性.
如果你有一个返回函数并处理大量数据的getter,那么Vuex无法帮助你缓存该函数调用的结果.你必须找到一种方法来最小化它被调用的次数,或者合并memoization等.