Ami*_*eol 1 javascript vue.js vuex vuejs2
vuex中有没有办法制作常量变量?我的文件结构
\n\nstore\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 actions.js \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mutations.js \nRun Code Online (Sandbox Code Playgroud)\n\n目前在我的index.js文件中,状态对象我有users包含以下内容的数组
'users': [{\n 'id': null,\n 'name': null,\n 'email': null,\n 'details': null\n }]\nRun Code Online (Sandbox Code Playgroud)\n\n在我的文件中,我有mutation.js突变方法addUsers
state.users.push(\n {\n 'id': null,\n 'name': null,\n 'email': null,\n 'details': null\n }\n )\nRun Code Online (Sandbox Code Playgroud)\n\n有没有办法重用这个初始用户属性对象?如何在 vuex 中使常量变量像这样?
\n您可以创建一个consts.js文件并将所有 const 放入其中:
export const USER = {
'id': null,
'name': null,
'email': null,
'details': null
};
export const FOO = 'bar';
Run Code Online (Sandbox Code Playgroud)
然后,您可以mutations.js使用以下两个导入语句之一将这些常量导入文件中:
import { USER } from 'path/to/consts.js'; // just user
import * as consts from 'path/to/consts.js'; // every single const
Run Code Online (Sandbox Code Playgroud)
并修改你的突变:
state.users.push(USER);
Run Code Online (Sandbox Code Playgroud)