programing

Vuex 및 vuex가 인수를 사용하는 getter

bestcode 2022. 7. 21. 00:37
반응형

Vuex 및 vuex가 인수를 사용하는 getter

저는 Vue와 Vuex를 기반으로 한 작은 앱을 가지고 있습니다.그런 것들이 있는 테이블일 뿐이야

<div class='items'>
  <div v-for='item in items'>
    <span> {{ item.name }} </span>
    <router-link :to='"update/" + item.id'>Edit</router-link>
  </div>
</div>

항목 배열은 Getter를 사용하여 Vuex 상태에서 로드됩니다.문제는 '편집' 버튼을 누르면 다른 페이지로 리다이렉트된다는 것입니다.이 페이지에는 이와 같은 기능이 있습니다.

computed() {
  item() {
    return this.$store.getters.get_item(this.$route.params.id)
  }
}

일반적으로 동작합니다(이것 대신 몇 가지 숫자를 전달하여 테스트했습니다).$route.params.id")는 아니지만, 이유는 무엇입니까?오류도 없고 아무것도 없습니다.어레이만 비어 있습니다.

get_item 함수

getters: {
  get_item: (state) => (index) => {
    return state.items.filter((item) => {
      return item.id === index
    }
  }
}

정의하고 있다computed함수로 사용할 수 있습니다.대신 시도:

computed: {
  item() {
    return this.$store.getters.get_item(this.$route.params.id)
  }
}

언급URL : https://stackoverflow.com/questions/47455303/vuex-and-vuex-getters-with-arguments

반응형