Skip to content
Advertisement

why is this Vuex state syntax throwing error?

I’m quite new with Vuejs & Vuex, I created a local project just to practice, so I have a file called: employeeList with an Array of objects. I’m trying to pass that same Array as state in Vuex, but is throwing me errors. I assume the syntax is wrong, please tell what would be the correct approach and if the problem is in fact the syntax. Thank you & here’s the code :

export const employeesModule = {
    namespaced: true,
    state : {
        [
            {
            id: 1, 
            name:'Terry Lawrence',
            username:'TerryLaw',
            email: 'TerryLaw@gmail.com',
            address: 'whateverStreet 258',
            checked: checked.value
            },
            {
            id: 2, 
            name:'MartyClFly',
            username:'MartyMac',
            email: 'MartyMac@gmail.com',
            address: 'George Junior 300',
            checked: checked.value
            },
            {
            id: 3, 
            name:'Nancy Pelosi',
            username:'Drunk ho',
            email: 'drunkHo@gmail.com',
            address: 'Velbedere 400',
            checked: checked.value
            }
]
            
    }, 

The actual file is longer, but there’s no purpose to add the mutations, actions, etc…

Advertisement

Answer

Your syntax is wrong on your object “state”. If you want it to be an object that contains an array, you need to set it a value like so:

state: {
  newArray: [...]
}

You can’t just have an object that contains an array and is not set with a key.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement