DjH*_*DjH 5 javascript vue.js vuex vuex-modules
例如,假设我有一个像这样的“store”目录:
\n\n...\nstore\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 auth\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 user.js\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n...\nRun Code Online (Sandbox Code Playgroud)\n\n索引.js
\n\nimport Vue from \'vue\';\nimport Vuex from \'vuex\';\nimport {user} from \'./auth/user\';\n\nVue.use(Vuex);\n\n/* eslint-disable no-new */\nconst store = new Vuex.Store({\n modules: {\n user\n },\n});\n\nexport default store;\nRun Code Online (Sandbox Code Playgroud)\n\n现在在user商店里我有一些常量和其他状态变量在它的state道具中。我如何state从其内部访问道具?例如user商店可能看起来像这样:
用户.js
\n\nexport const user = {\n namespaced: true,\n\n state: {\n\n // hardcoded string assigned to user.state.constants.SOME_CONST\n constants: {\n SOME_CONST: \'testString\'\n },\n\n // Another property where I would like to reference the constant above\n\n someOtherStateProp: {\n\n // Trying to access the constant in any of these ways throws\n // \'Uncaught ReferenceError: .... undefined\'\n // Where \'...\' above is interchangeable with any root I try to access the constant from (this, state etc)\n\n test1: this.state.constants.SOME_CONST,\n test2: user.state.constants.SOME_CONST\n test3: state.constants.SOME_CONST\n test4: constants.SOME_CONST\n test5: SOME_CONST\n // .... etc. All the above throw ReferenceError\'s \n }\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n我怎样才能user.state.constants.SOME_CONST参考user.state.someOtherStateProp.test1?
感觉我在这里错过了一些非常基本的东西。
\n最简单的方法是CONSTANTS在导出模块之前声明对象并访问它们,如下所示
const CONSTANTS = {
SOME_CONST: 'testString'
}
export const user = {
namespaced: true,
state: {
// hardcoded string assigned to user.state.constants.SOME_CONST
constants: CONSTANTS,
// Another property where I would like to reference the constant above
someOtherStateProp: {
test1: CONSTANTS.SOME_CONST,
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2296 次 |
| 最近记录: |