programing

Vue/Nuxt 라이프사이클에서 사전 생성 및 생성된 Getter를 호출함

prostudy 2022. 5. 22. 11:24
반응형

Vue/Nuxt 라이프사이클에서 사전 생성 및 생성된 Getter를 호출함

구성요소에 대해 beforeCreate()에서 변수 집합을 수행한 후 생성된()에서 해당 변수를 가져오려고 시도하고 있다.본질적으로 내 상점에서 변수를 설정하고 변수를 가져와야 해.내가 만든 것은 국가에 데이터를 위탁하는 공리 약속을 실행하는 행위를 부르고, 나는 그 데이터를 얻기 위해 내가 만든()을 불러들이고 싶은 가게의 게터를 가지고 있다.

내 콘솔 로그에서 볼 수 있듯이 내 약속에서 정확한 데이터를 되찾고 있다는 것을 알고 있어, 나는 단지 그 데이터를 얻는 사람으로부터 얻을 수 없는 것 같아.

효과를 보고 싶지 않은 것 같고, 공리들이 아직 그 약속을 돌려주지 않은 사실과 관계가 있다고 생각한다.Create() 전에 공리 약속을 반환할 때까지 내가 만든()을 기다리게 하려면 어떻게 해야 하는가?

샘플코드

..구성요소....

data(){
  return{currentUser: null}
},
beforeCreate(){
  this.$store.dispatch('users/setCurrent')
}, 
created(){
 this.currentUser = this.$store.getters['users/current'].displayname
}

......점포...

export const state = () => ({
  current: {}
});

export const mutations ={
  setCurrent: (state, c) => (state.current = c)
}

export const getter = {
  current: state => state.current
}

export const actions = {
setCurrent({commit, dispatch, rootState}){
   this.$axios.$get('/api/user/current', {headers: rootState.sessionUser.current})
   .then(res => {
     console.log(res)
     commit("setCurrent" res)
  })
}

다음 작업에서 응답을 기다리십시오.

export const actions = {
  async setCurrent({commit, dispatch, rootState}){
    await this.$axios.$get('/api/user/current', {headers: rootState.sessionUser.current})
      .then(res => {
      console.log(res)
      commit("setCurrent" res)
  })
}

생성되거나 마운트된 후크:

async created() {
  await this.$store.dispatch('users/setCurrent')
  this.currentUser = this.$store.getters['users/current'].displayname
}
  

참조URL: https://stackoverflow.com/questions/69497736/vue-nuxt-lifecycle-calling-a-promise-in-beforecreate-and-the-getter-in-created

반응형