반응형
Vuex 및 Composition API를 사용하여 사후 대응 속성에 액세스할 수 있는 방법이 있습니까?
내 컴포넌트를 다음과 같이 설계할 경우:
<template>
<div>
<button @click="increment">Count is: {{ store.getters.count }}</button>
</div>
</template>
<script>
import { reactive } from "@vue/composition-api";
import store from "../store";
export default {
name: "Count",
setup() {
const state = reactive({
store
});
const increment = () => {
store.dispatch.increment();
};
return {
...state,
increment
};
}
};
</script>
그리고 제 스토어는 다음과 같이 정의됩니다.
import Vue from "vue";
import Vuex from "vuex";
import { createDirectStore } from "direct-vuex";
Vue.use(Vuex);
const {
store,
rootActionContext,
moduleActionContext,
rootGetterContext,
moduleGetterContext
} = createDirectStore({
state: {
count: 0
},
getters: {
count: state => state.count
},
mutations: {
increment(state) {
state.count++;
}
},
actions: {
increment(context) {
context.commit("increment");
}
}
});
// Export the direct-store instead of the classic Vuex store.
export default store;
// The following exports will be used to enable types in the
// implementation of actions and getters.
export {
rootActionContext,
moduleActionContext,
rootGetterContext,
moduleGetterContext
};
// The following lines enable types in the injected store '$store'.
export type AppStore = typeof store;
declare module "vuex" {
interface Store<S> {
direct: AppStore;
}
}
카운트에 더 잘 접근할 수 있는 방법은 없을까?{{ store.getters.count }}
템플릿에 저장하시겠습니까?이상적으로는 다음과 같이 접근하고 싶습니다.{{ count }}
, 그러나 그것은 단지store
반응합니다.즉, 증분 액션을 디스패치하면 카운트를 다양한 방법으로 정의하려고 해도 "{ count }}"이 갱신되지 않습니다.제가 시도한 것은 다음과 같습니다.
setup() {
const state = reactive({
store,
count: store.getters.count
});
const increment = () => {
store.dispatch.increment();
};
return {
...state,
count: state.count,
increment
};
}
왜?{{ count }}
반응할 수 있을까요?
count: store.getters.count
현재 값을 저장하고 있는 것을 의미합니다.store.getters.count
디폴트값으로서count
.
반응하지 않을 거란 뜻이죠주의해 주세요count
스토어 내에는 함수가 있습니다.
네 입장을 밝혀볼 수도 있어count
적절하게 갱신할 수 있도록 계산된 속성을 대신 사용합니다.
Composition API는 아직 시도하지 않았지만 도움이 되었으면 합니다.
언급URL : https://stackoverflow.com/questions/60498766/using-vuex-and-the-composition-api-is-there-a-way-to-access-reactive-properties
반응형
'programing' 카테고리의 다른 글
Laravel 프로젝트의 Vue.use(VueResource) 문자열은 어디에 있습니까? (0) | 2022.08.02 |
---|---|
Java에서 jsonString을 JSONObject로 변환하는 방법 (0) | 2022.08.02 |
Java에서 정적 메서드를 추상화할 수 없는 이유는 무엇입니까? (0) | 2022.08.02 |
vue v-model 지시문의 개체 경로로 점 구분 문자열을 사용하는 방법 (0) | 2022.08.02 |
어떻게 만들 수 있는 부트 스트랩 vue에 단추 마이너스와 플러스? (0) | 2022.08.02 |