What if the data in vuex cannot be obtained in the component?

problem description

I am working on the topic display page of cnodejs. Cnode-article-vx.vue and author.vue have a common parent component, detail.vue,cnode-article-vx.vue, which notifies vuex to send a request to get the topic data and save it in vuex. In
author.vue, I want to get the author in the topic data stored in vuex, and then send a request to get the user data. But I was wrong to get the author"s Times.

related codes

cnode-article-vx.vue
methods: {
/ / notify vuex to get a detailed view of the article and pass the author"s name up and reply

getDetailVuex(){
  this.$store.commit("showSpinMu", {show: true});

  let id = this.$route.params.id;
  let accesstoken = Cookies.get("accesstoken");

  if(accesstoken){
    //
    this.$store.dispatch("getDetailAc", {id, accesstoken});
  }else{
    //
    this.$store.dispatch("getDetailAc", {id});
  }
}

}

vuex
actions: {
/ / get an overview of articles

    getDetailAc(store, payload) {
        let accesstoken = payload.accesstoken ? payload.accesstoken : "";

        return getDetail(payload.id, { accesstoken }).then(({ data }) => {
            store.commit("detailMu", { detail: data.data });
        });
    }

},
mutations: {
/ / articles

    detailMu(state, payload) {
        state.detail = payload.detail;
        state.spinShow = false;
        console.log(state.detail.author.loginname);
    }

}

author.vue
computed: {
/ / get the author name from vuex

authorName(){
  return this.$store.state.detail.author.loginname;
}

}

what result do you expect? What is the error message actually seen?

the error that can print out the loginname,author.vue report in vuex is as follows:
Uncaught (in promise) TypeError: Cannot read property "loginname" of undefined
how to solve it?


The

subcomponent evaluation attribute is executed first. At that time, the state.detail has not been assigned, that is, it does not have an author or loginname attribute, so perform a commit assignment before return in the authorName. For example:

computed: {
  // vuex 
  authorName(){
    this.$store.commit('detailMu',{detial:{author:{loginname:"456"}}});
    return this.$store.state.detail.author.loginname;
  }
}
Menu