programing

VueX Store에서 계산된 속성으로 Ajax를 호출하는 올바른 방법은 무엇입니까?

css3 2023. 6. 9. 22:18

VueX Store에서 계산된 속성으로 Ajax를 호출하는 올바른 방법은 무엇입니까?

VueX에서 계산된 상태인 호출 매개 변수 중 하나를 사용하여 Ajax 호출을 수행하려면 어떻게 해야 합니까?예를 들어, 제가 이걸 만든다면요.$axios.get('someUrl/' + acc)ID), accID는 (MapState를 통해) VueX에서 계산된 속성입니다.때때로 id가 'undefined'를 반환하는데, 이는 id 데이터가 스토어에서 채워지기 전에 get 요청을 하는 axios 때문인 것으로 생각됩니다.

저는 'acc'에서 시계()를 사용해 보았습니다.vue 구성 요소에서 accID가 확인될 때까지 기다리지만 사용할 수 없습니다.

//일부 코드

 computed: {
    ...mapState(['user']),
  },

 async fetchData() {


        const [foodData, option] = await Promise.all([
          this.$axios({
            url: `food/${this.user.accID}`,
            method: 'get',
          }),
          this.$axios({
            url: 'options',
            method: 'get',
          })
        ])

  //foodData returns undefined because user.accID is undefined (sometimes)

기대하는

this.$getos({url: 'food/12345', 방법: 'get' }

대신

이.$getos url: 'food/http', 방법: 'get' }

바꾸다${user.accID}로.${this.user.accID}url:

url: `food/${this.user.accID}`,

언급URL : https://stackoverflow.com/questions/55584802/what-is-the-proper-way-to-do-ajax-call-with-computed-properties-from-vuex-store