Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问';这';处于Vuex存储模块状态_Javascript_Vue.js_Vuex_Vuex Modules - Fatal编程技术网

Javascript 如何访问';这';处于Vuex存储模块状态

Javascript 如何访问';这';处于Vuex存储模块状态,javascript,vue.js,vuex,vuex-modules,Javascript,Vue.js,Vuex,Vuex Modules,例如,假设我有一个“存储”目录,如下所示: ... store ├── auth │   └── user.js └── index.js ... index.js import Vue from 'vue'; import Vuex from 'vuex'; import {user} from './auth/user'; Vue.use(Vuex); /* eslint-disable no-new */ const store = new Vuex.Store({ module

例如,假设我有一个“存储”目录,如下所示:

...
store
├── auth
│   └── user.js
└── index.js
...
index.js

import Vue from 'vue';
import Vuex from 'vuex';
import {user} from './auth/user';

Vue.use(Vuex);

/* eslint-disable no-new */
const store = new Vuex.Store({
  modules: {
    user
  },
});

export default store;
export const user = {
  namespaced: true,

  state: {

    // hardcoded string assigned to user.state.constants.SOME_CONST
    constants: {
      SOME_CONST: 'testString'
    },

    // Another property where I would like to reference the constant above

    someOtherStateProp: {

      // Trying to access the constant in any of these ways throws
      // 'Uncaught ReferenceError: .... undefined'
      // Where '...' above is interchangeable with any root I try to access the constant from (this, state etc)

      test1: this.state.constants.SOME_CONST,
      test2: user.state.constants.SOME_CONST
      test3: state.constants.SOME_CONST
      test4: constants.SOME_CONST
      test5: SOME_CONST
      // .... etc. All the above throw ReferenceError's 
    }
  }
};
现在在
user
存储中,我在它的
state
prop中有一些常量和其他状态变量。如何从道具本身访问
状态
道具?例如,
用户
商店可能如下所示:

...
store
├── auth
│   └── user.js
└── index.js
...
user.js

import Vue from 'vue';
import Vuex from 'vuex';
import {user} from './auth/user';

Vue.use(Vuex);

/* eslint-disable no-new */
const store = new Vuex.Store({
  modules: {
    user
  },
});

export default store;
export const user = {
  namespaced: true,

  state: {

    // hardcoded string assigned to user.state.constants.SOME_CONST
    constants: {
      SOME_CONST: 'testString'
    },

    // Another property where I would like to reference the constant above

    someOtherStateProp: {

      // Trying to access the constant in any of these ways throws
      // 'Uncaught ReferenceError: .... undefined'
      // Where '...' above is interchangeable with any root I try to access the constant from (this, state etc)

      test1: this.state.constants.SOME_CONST,
      test2: user.state.constants.SOME_CONST
      test3: state.constants.SOME_CONST
      test4: constants.SOME_CONST
      test5: SOME_CONST
      // .... etc. All the above throw ReferenceError's 
    }
  }
};
如何从
user.state.someOtherStateProp.test1
引用
user.state.constants.SOME_CONST


感觉我在这里遗漏了一些非常基本的东西。

你可以通过两个步骤来完成

let user = {
    namespaced: true,
    state: {
        SOME_CONST: 'testString'
    }
};

Object.assign(user, {
    state: {
        someOtherStateProp: {
            test1: user.state.SOME_CONST
        }
    }
});

export default user;

阅读有关
对象的更多信息。在此处分配
-

最简单的方法是在导出模块和访问它们之前,先对
常量进行去极化,如下所示

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,
    }
  }
}; 

是否要在任何范围内访问这些常量components@VamsiKrishna,是的,这目前不是问题。在映射组件
computed
props中的状态后,我能够在组件中很好地访问它们