programing

store에서 string vue.display로 상태 데이터에 액세스합니다.

coolbiz 2022. 8. 28. 21:38
반응형

store에서 string vue.display로 상태 데이터에 액세스합니다.

여러분, 그래서 저는 제 상점의 상태 데이터를 액션 내 악리콜 중 하나의 문자열에 추가하려고 합니다.제 매장은 이쪽입니다.

export const store = new Vuex.Store
({
  state: {
    participants: [],
    filterTags: ["foo", "swag"],
    page: 1,
    perPage: 2,
    filterTagName: "",
   }
}

액션 콜은 다음과 같습니다.

 actions: {
async loadParticipants ({commit}) {
  try {
    console.log(this.state.page);
    await axios
  .get('/dash/participant?perPage=2&page=1&string=f')

      .then(r => r.data)
      .then(participants => {
        console.log(participants.docs);
        console.log("Hit me");
        commit('setParticipants', participants)
      })
  }
  catch (e) {
    if (e.response)
      console.log(e.response);
    throw e
  }
}

Axios 호출에 {INSERT DATA HERE}이라고 표시된 스토어 상태 데이터를 추가합니다.

 .get('/dash/participant?perPage={INSERT DATA HERE }&page={ INSERT DATA HERE }&string=f')

입력해 주셔서 감사합니다!

액션에서는 스토어 전체에 액세스 할 수 있기 때문에 파라미터가 다음과 같이 선언되는 커밋만 취득할 수 있습니다.({commit}), 상태를 추가할 수도 있습니다.

async loadParticipants ({commit, state}) {

이 경우 를 사용할 수 있습니다.state메서드 본문의 변수:

actions: {
  async loadParticipants ({commit, state}) {
    try {
      console.log(this.state.page);
      await axios
    .get(`/dash/participant?perPage=${state.perPage}&page=${state.page}&string=${state.filterTagName}`)

        .then(r => r.data)
        .then(participants => {
          console.log(participants.docs);
          console.log("Hit me");
          commit('setParticipants', participants)
        })
    }
    catch (e) {
      if (e.response)
        console.log(e.response);
      throw e
    }
  }
}

따라서 쿼리 매개 변수를 vuex 스토어의 값으로 채우려는 것입니까?그냥 지나쳐요state행동으로 옮겨야 합니다.그런 다음 템플릿을 사용하여 상태를 쿼리 매개 변수에 추가할 수 있습니다. ${some-js-variable}

응답을 직접 파기하고 데이터를 취득할 수도 있습니다.왜 그런 약속을 하는지 모르겠다.then()문(사용하는 경우)async그리고.await.

actions: {
 async loadParticipants ({commit, state}) {
  try {
    const {data} = await axios.get(`/dash/participant?perPage=${state.perPage}&page=${state.page}&string=f`)
    console.log(data)

  }
  catch (e) {
    if (e.response)
      console.log(e.response);
    throw e
  }
}

언급URL : https://stackoverflow.com/questions/51641388/access-state-data-from-store-into-a-string-vue-js

반응형