Vuex Modules Creating Nested Object









up vote
0
down vote

favorite












I have been trying to separate my store.js file into multiple store modules. I have it working (somewhat), but it also created kind of a big issue.



In my initial one single file store, let's suppose this is my state object:



state: user: '' 


And the output in the Vuex developer tools shows the user object as it should:



User: name: "John", age: 33 


But after splitting the the store into modules the user object in the Vuex developer tools is as follows:



User: User: name: "John", age: 33 


So it creates a new object using the name I give it in the modules object and import in the main store.js file, which then contains the "true" user object.



Is there any way to have it not create a new object and keep it the same as if the entire store lived inside the store.js file?



-Sergio



EDIT:



Here is the structure of my files:

store
store.js
modules
user.js



The code in the modules/user.js:



const state = User: '' 
const mutations = ...
const actions = ...
export default state, mutations, actions


The code in my store.js



import User from './modules/user';
Vue.use(Vuex);
export default new Vuex.Store(
modules:
User

);


Prior to externalizing the store I would get in Vuex dev tools:
User: name: "John", age: 33
After externalizing the store I get:
User: User: name: "John", age: 33



It's nesting User from the modules/user file inside the User in the store.js file. I want to avoid that (if its possible) so that I don't have to change everything in my components/template files.










share|improve this question























  • But how are you doing this assignment?
    – Hamilton Gabriel
    13 hours ago










  • Hi Hamilton, I don't understand your question...can you please rephrase.
    – Sergio
    13 hours ago










  • At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
    – Hamilton Gabriel
    12 hours ago










  • I have updated my initial question, hope it clarifies more.
    – Sergio
    12 hours ago










  • Are you assigning the user again in vuex, or are you doing this yourself?
    – Hamilton Gabriel
    12 hours ago














up vote
0
down vote

favorite












I have been trying to separate my store.js file into multiple store modules. I have it working (somewhat), but it also created kind of a big issue.



In my initial one single file store, let's suppose this is my state object:



state: user: '' 


And the output in the Vuex developer tools shows the user object as it should:



User: name: "John", age: 33 


But after splitting the the store into modules the user object in the Vuex developer tools is as follows:



User: User: name: "John", age: 33 


So it creates a new object using the name I give it in the modules object and import in the main store.js file, which then contains the "true" user object.



Is there any way to have it not create a new object and keep it the same as if the entire store lived inside the store.js file?



-Sergio



EDIT:



Here is the structure of my files:

store
store.js
modules
user.js



The code in the modules/user.js:



const state = User: '' 
const mutations = ...
const actions = ...
export default state, mutations, actions


The code in my store.js



import User from './modules/user';
Vue.use(Vuex);
export default new Vuex.Store(
modules:
User

);


Prior to externalizing the store I would get in Vuex dev tools:
User: name: "John", age: 33
After externalizing the store I get:
User: User: name: "John", age: 33



It's nesting User from the modules/user file inside the User in the store.js file. I want to avoid that (if its possible) so that I don't have to change everything in my components/template files.










share|improve this question























  • But how are you doing this assignment?
    – Hamilton Gabriel
    13 hours ago










  • Hi Hamilton, I don't understand your question...can you please rephrase.
    – Sergio
    13 hours ago










  • At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
    – Hamilton Gabriel
    12 hours ago










  • I have updated my initial question, hope it clarifies more.
    – Sergio
    12 hours ago










  • Are you assigning the user again in vuex, or are you doing this yourself?
    – Hamilton Gabriel
    12 hours ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have been trying to separate my store.js file into multiple store modules. I have it working (somewhat), but it also created kind of a big issue.



In my initial one single file store, let's suppose this is my state object:



state: user: '' 


And the output in the Vuex developer tools shows the user object as it should:



User: name: "John", age: 33 


But after splitting the the store into modules the user object in the Vuex developer tools is as follows:



User: User: name: "John", age: 33 


So it creates a new object using the name I give it in the modules object and import in the main store.js file, which then contains the "true" user object.



Is there any way to have it not create a new object and keep it the same as if the entire store lived inside the store.js file?



-Sergio



EDIT:



Here is the structure of my files:

store
store.js
modules
user.js



The code in the modules/user.js:



const state = User: '' 
const mutations = ...
const actions = ...
export default state, mutations, actions


The code in my store.js



import User from './modules/user';
Vue.use(Vuex);
export default new Vuex.Store(
modules:
User

);


Prior to externalizing the store I would get in Vuex dev tools:
User: name: "John", age: 33
After externalizing the store I get:
User: User: name: "John", age: 33



It's nesting User from the modules/user file inside the User in the store.js file. I want to avoid that (if its possible) so that I don't have to change everything in my components/template files.










share|improve this question















I have been trying to separate my store.js file into multiple store modules. I have it working (somewhat), but it also created kind of a big issue.



In my initial one single file store, let's suppose this is my state object:



state: user: '' 


And the output in the Vuex developer tools shows the user object as it should:



User: name: "John", age: 33 


But after splitting the the store into modules the user object in the Vuex developer tools is as follows:



User: User: name: "John", age: 33 


So it creates a new object using the name I give it in the modules object and import in the main store.js file, which then contains the "true" user object.



Is there any way to have it not create a new object and keep it the same as if the entire store lived inside the store.js file?



-Sergio



EDIT:



Here is the structure of my files:

store
store.js
modules
user.js



The code in the modules/user.js:



const state = User: '' 
const mutations = ...
const actions = ...
export default state, mutations, actions


The code in my store.js



import User from './modules/user';
Vue.use(Vuex);
export default new Vuex.Store(
modules:
User

);


Prior to externalizing the store I would get in Vuex dev tools:
User: name: "John", age: 33
After externalizing the store I get:
User: User: name: "John", age: 33



It's nesting User from the modules/user file inside the User in the store.js file. I want to avoid that (if its possible) so that I don't have to change everything in my components/template files.







vue.js store vuex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago

























asked 13 hours ago









Sergio

356112




356112











  • But how are you doing this assignment?
    – Hamilton Gabriel
    13 hours ago










  • Hi Hamilton, I don't understand your question...can you please rephrase.
    – Sergio
    13 hours ago










  • At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
    – Hamilton Gabriel
    12 hours ago










  • I have updated my initial question, hope it clarifies more.
    – Sergio
    12 hours ago










  • Are you assigning the user again in vuex, or are you doing this yourself?
    – Hamilton Gabriel
    12 hours ago
















  • But how are you doing this assignment?
    – Hamilton Gabriel
    13 hours ago










  • Hi Hamilton, I don't understand your question...can you please rephrase.
    – Sergio
    13 hours ago










  • At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
    – Hamilton Gabriel
    12 hours ago










  • I have updated my initial question, hope it clarifies more.
    – Sergio
    12 hours ago










  • Are you assigning the user again in vuex, or are you doing this yourself?
    – Hamilton Gabriel
    12 hours ago















But how are you doing this assignment?
– Hamilton Gabriel
13 hours ago




But how are you doing this assignment?
– Hamilton Gabriel
13 hours ago












Hi Hamilton, I don't understand your question...can you please rephrase.
– Sergio
13 hours ago




Hi Hamilton, I don't understand your question...can you please rephrase.
– Sergio
13 hours ago












At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
– Hamilton Gabriel
12 hours ago




At what point in time do you do this assignment? And how is this assignment being done? Are you calling this assignment more than once?
– Hamilton Gabriel
12 hours ago












I have updated my initial question, hope it clarifies more.
– Sergio
12 hours ago




I have updated my initial question, hope it clarifies more.
– Sergio
12 hours ago












Are you assigning the user again in vuex, or are you doing this yourself?
– Hamilton Gabriel
12 hours ago




Are you assigning the user again in vuex, or are you doing this yourself?
– Hamilton Gabriel
12 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Given your initial store state:



state: user: '' 


And an object with the following shape representing a user:



 name: "John", age: 33 


You can write a mutation as:



SET_USER: (state, user) => 
Object.assign(state, user )



Commit the user:



this.$store.commit('SET_USER', user)


This is assuming no module namespaces of course.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236866%2fvuex-modules-creating-nested-object%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Given your initial store state:



    state: user: '' 


    And an object with the following shape representing a user:



     name: "John", age: 33 


    You can write a mutation as:



    SET_USER: (state, user) => 
    Object.assign(state, user )



    Commit the user:



    this.$store.commit('SET_USER', user)


    This is assuming no module namespaces of course.






    share|improve this answer
























      up vote
      0
      down vote













      Given your initial store state:



      state: user: '' 


      And an object with the following shape representing a user:



       name: "John", age: 33 


      You can write a mutation as:



      SET_USER: (state, user) => 
      Object.assign(state, user )



      Commit the user:



      this.$store.commit('SET_USER', user)


      This is assuming no module namespaces of course.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Given your initial store state:



        state: user: '' 


        And an object with the following shape representing a user:



         name: "John", age: 33 


        You can write a mutation as:



        SET_USER: (state, user) => 
        Object.assign(state, user )



        Commit the user:



        this.$store.commit('SET_USER', user)


        This is assuming no module namespaces of course.






        share|improve this answer












        Given your initial store state:



        state: user: '' 


        And an object with the following shape representing a user:



         name: "John", age: 33 


        You can write a mutation as:



        SET_USER: (state, user) => 
        Object.assign(state, user )



        Commit the user:



        this.$store.commit('SET_USER', user)


        This is assuming no module namespaces of course.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 12 hours ago









        DigitalDrifter

        5,7142422




        5,7142422



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236866%2fvuex-modules-creating-nested-object%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands