Vuex "context" 对象是什么?
What is the Vuex "context" object?
我试图更好地了解 Vuex 中的 "context" 对象是什么。
上下文对象 在 Vuex 文档中被多次提及。例如,在 https://vuex.vuejs.org/en/actions.html 中,我们有:
Action handlers receive a context object which exposes the same set of
methods/properties on the store instance, so you can call
context.commit to commit a mutation...
我了解如何使用它,而且如果我们只想使用上下文对象中的 "commit",我们可以使用解构,但我希望更深入一点,这样我才能更好地理解发生了什么。
首先,我在 "context object" 上找到了一些 ~8.5 年前的帖子作为模式:
what is context object design pattern? 和
Can you explain the Context design pattern?
但是,具体到 Vuex,我希望更好地理解:
- 上下文对象是什么/它的用途是什么?
- 它可以在 Vuex 中使用的 properties/methods 有哪些?
谢谢!
As a start, I found a couple ~8.5 year old posts on the "context object" as a pattern ...
我觉得你读的太多了。
我不认为 Vuex 文档指的是在其他地方已知和定义的某种特定类型的 "context object",它们只是表示传递给操作处理程序的对象(在其他情况下为在文档中描述)是一个自定义对象,他们根据他们自己的定义将其称为 "context" 对象。
他们提供此对象的原因是因为它包含特定于特定操作处理程序的 module 的属性。
从您指出的documentation中可以读到:
We will see why this context object is not the store instance itself when we introduce Modules later.
上下文对象的主要思想是抽象当前Module的范围。如果只是简单地访问store.state
,它永远是根状态。
描述了动作的上下文对象及其properties/methodshere in the source code and also referenced in the API documentation
列表如下:
{
state, // same as store.state, or local state if in modules
rootState, // same as store.state, only in modules
commit, // same as store.commit
dispatch, // same as store.dispatch
getters, // same as store.getters, or local getters if in modules
rootGetters // same as store.getters, only in modules
}
根据vuex的源码,context只是一个文字对象,一些属性来自local,其他属性来自store。
我试图更好地了解 Vuex 中的 "context" 对象是什么。
上下文对象 在 Vuex 文档中被多次提及。例如,在 https://vuex.vuejs.org/en/actions.html 中,我们有:
Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call context.commit to commit a mutation...
我了解如何使用它,而且如果我们只想使用上下文对象中的 "commit",我们可以使用解构,但我希望更深入一点,这样我才能更好地理解发生了什么。
首先,我在 "context object" 上找到了一些 ~8.5 年前的帖子作为模式: what is context object design pattern? 和 Can you explain the Context design pattern?
但是,具体到 Vuex,我希望更好地理解:
- 上下文对象是什么/它的用途是什么?
- 它可以在 Vuex 中使用的 properties/methods 有哪些?
谢谢!
As a start, I found a couple ~8.5 year old posts on the "context object" as a pattern ...
我觉得你读的太多了。
我不认为 Vuex 文档指的是在其他地方已知和定义的某种特定类型的 "context object",它们只是表示传递给操作处理程序的对象(在其他情况下为在文档中描述)是一个自定义对象,他们根据他们自己的定义将其称为 "context" 对象。
他们提供此对象的原因是因为它包含特定于特定操作处理程序的 module 的属性。
从您指出的documentation中可以读到:
We will see why this context object is not the store instance itself when we introduce Modules later.
上下文对象的主要思想是抽象当前Module的范围。如果只是简单地访问store.state
,它永远是根状态。
描述了动作的上下文对象及其properties/methodshere in the source code and also referenced in the API documentation
列表如下:
{
state, // same as store.state, or local state if in modules
rootState, // same as store.state, only in modules
commit, // same as store.commit
dispatch, // same as store.dispatch
getters, // same as store.getters, or local getters if in modules
rootGetters // same as store.getters, only in modules
}
根据vuex的源码,context只是一个文字对象,一些属性来自local,其他属性来自store。