반응형
Composition API를 사용하여 Vuex name leted getter를 사용하는 방법
나는 현재 그것을 회수하려고 하고 있다.tags
Vuex에서 Searchbar 모듈로.그러나 그것은 반응적이지 않다.
다음은 구성 요소:
<template>
<div class="tags">
<tag v-for="tag in tags" :key="tag.name">{{ tag.name }}</tag>
</div>
</template>
import { defineComponent, computed } from '@vue/composition-api';
import store from '@/store/index';
import Tag from '@/components/BaseTag.vue';
export default defineComponent({
components: {
Tag
},
setup() {
const tags = computed(() => store.getters['Searchbar/all']);
return {
tags
};
}
});
및 vuex 모듈
import { Module, VuexModule, Mutation } from 'vuex-module-decorators';
import { TagType } from '@/enums';
type VuexTag = { name: string; type: TagType };
@Module({
namespaced: true
})
export default class Searchbar extends VuexModule {
private tagsInput: Array<VuexTag> = [];
get all(): Array<VuexTag> {
return this.tagsInput;
}
@Mutation
addTag(tag: VuexTag): void {
this.tagsInput[this.tagsInput.length] = tag;
}
@Mutation
removeTag(index: number): void {
this.tagsInput.splice(index, 1);
}
}
왜인지 모르겠다.나는 Typecript를 사용하고 있다. 그래서 그것은 다음을 지원하지 않는다.store.Searchbar.getters['all'])
좋은 생각이 있어?
그래, 내가 문제를 찾았어.내 잘못은 컴포지션 API와는 아무 상관이 없다.상기의 코드가 그것에 대해 작용하고 있다.
Vuex 모듈에서, 나는 업데이트 해야 했다.addTag
다음과 같은 돌연변이:
@Mutation
addTag(tag: SearchbarTag): void {
Vue.set(this.tagsInput, this.tagsInput.length, tag);
}
기본적으로 어레이 변경은 기본적으로 반응하지 않으므로Vue.set
방법의
반응형
'Programing' 카테고리의 다른 글
vue.js에서 onfocusout 기능을 사용하는 방법? (0) | 2022.04.12 |
---|---|
페이지 로드 시 기능을 호출하기 위해 Vue.js2에서 사용할 라이프사이클 후크를 선택하십시오. (0) | 2022.04.12 |
VueJS $store.dispatch에서 여러 매개 변수 전송 (0) | 2022.04.12 |
Jest를 사용하여 VueJS 구성 요소의 사용자 지정 모듈 조롱 (0) | 2022.04.12 |
VueJS/VueX 최대 통화 스택 크기를 초과함 (0) | 2022.04.12 |