Amitoz Deol
Amitoz Deol

Reputation: 442

Create constant variables in vuex?

Is there a way to make a constant variable in vuex? My file structure

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

Currently in my index.js file, state object I have users array that contains

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

And in my mutation.js file, mutation method addUsers I have

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

Is there a way to reuse this initial user property object? How do I make constants variable like this in vuex?

Upvotes: 1

Views: 1986

Answers (1)

enbermudas
enbermudas

Reputation: 1615

You could make a consts.js file and put all your const inside of it:

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

export const FOO = 'bar';

Then you can import those consts inside your mutations.js file by using one of these two import statements:

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

And modify your mutations:

state.users.push(USER);

Upvotes: 5

Related Questions