On the implementation of computed and mounted in vue

get the attributes in vuex in computed in the vue component, and then send the request using the properties in state in the created life hook. It is found that the parameters are not obtained when mounted sends the request, and the page is updated only after it is correct:

computed: {
      ...(mapState({
        user_name: state => state.user_name,
        user_id: state => state.user_id,
        user_source: state => state.user_source,
      }))
    }
    
created() {
      request(extend(true, {}, apis.getUserConsultInfo, {
        params: {
          consult_id: this.consult_id,
          user_id: this.user_id,
          user_source: this.user_source
        }
      })).then((res) => {
        console.log(res);
      }, (errmsg) => {
        this.$message.error(errmsg);
      });
    }

how can this situation be broken? Ask the great god to solve ~

Feb.28,2021

any data that needs to be processed in the getter of vuex is done in the beforeUpdate or update phase.

an experiment was done 800 years ago: take it.

< H1 > 11 lifecycle print calculation attribute values (the first 6 have output, the last 5 have no output) < / H1 >

print the calculated property results obtained from the getter of vuex for each lifecycle.
print attempts in the mounted phase, and the rest are similar:

    mounted(){
        console.log("mounted")
        console.log(this.users)
    }

data obtained from vuex getter:

    computed:mapGetters({
        users:'allUsers'
    }),

print result:
image

after observation, it is found that of the 11 phases of the vue component life cycle, only the first 6 phases have output results.
beforeDestroy,destroyed,activated,deactivated,errorCaptured has no output results.
the initial output of the first six stages is not valid data, and the undefined of beforeCreate and the observer, of other stages are invalid data.
the valid data is not obtained until 16-28-33-740, and the data is only obtained during the before and updated phases.

We draw a conclusion from the experimental results: all the data in the getter of vuex need to be processed in the beforeUpdate or update phase.

Menu