在vuex中创建常量变量?

阿米托兹·多尔(Amitoz Deol)

有没有办法在vuex中制作一个常量变量?我的档案结构

store
    ├── index.js          
    ├── actions.js        
    ├── mutations.js      

当前在我的index.js文件中,状态对象我有users包含以下内容的数组

'users': [{
     'id': null,
     'name': null,
     'email': null,
     'details': null
 }]

在我的mutation.js文件中,addUsers我有突变方法

 state.users.push(
   {
      'id': null,
      'name': null,
      'email': null,
      'details': null
   }
 )

有没有办法重用此初始用户属性对象?我如何在vuex中使这样的常量变量?

恩里克·贝穆德斯(Enrique Bermudez)

您可以制作一个consts.js文件,并将所有const放入其中:

export const USER = {
    'id': null,
    'name': null,
    'email': null,
    'details': null
};

export const FOO = 'bar';

然后,您可以mutations.js使用以下两个import语句之一将这些const导入文件中:

import { USER } from 'path/to/consts.js'; // just user
import * as consts from 'path/to/consts.js'; // every single const

并修改您的突变:

state.users.push(USER);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章