Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 자바 공배수
- 자바 반복문
- 자바 while문
- 자바 switch문
- 정보처리기사실기
- 자바 강제 캐스팅
- 변수
- TypeScript
- 이클립스 DB연동
- 자바
- react with typescript
- 자바 삼항연산자
- Til
- 조코딩
- react ag grid
- 자바 향상된 for문
- 항해99 2기
- 항해99
- 자바 if문
- 프로그래머스
- 자바 for문
- Vue3
- 자바 구구단 출력
- 자바 public
- 자바 조건문
- 자바 스캐너
- 타입스크립트
- 자바 자동캐스팅
- MySQL
- java
Archives
- Today
- Total
뇌 채우기 공간
[VUE.js] vue3 vuex module 분리/ 파일에서 접근하는 법 본문
Vue3에서 vuex module분리하는 법
어려운 작업이 아닌데 처음에 할 때 vue3 관련 코드가 없어서 많이 헤맸던 기억이 있다.
1. store폴더 안에 modules폴더를 만든다.
2. 그 안에 모듈 하나씩 파일을 만든다.
예시 : user.ts
import {
SavedMovie, User, Collection, SavedCollection
} from '@/type';
import {
SET_USER,
SET_PASTMOVIE, SET_FUTUREMOVIE,
...
} from '../mutation';
export interface State {
user: User,
pastMovie : SavedMovie[],
futureMovie : SavedMovie[],
collections : Collection[],
savedCollections : SavedCollection[]
}
const user = {
namespaced: true,
state: {
user: {},
pastMovie: [],
futureMovie: [],
collections: [],
savedCollections: []
},
getters: {
isSavedCollection: (state) => (id:number) => {
const result = state.savedCollections.some((p) => p.collectionId === id);
return result;
},
savedId: (state) => (collectionId:number) => {
const result = state.savedCollections.filter((p) => p.collectionId === collectionId);
return result[0].id;
}
},
mutations: {
[SET_USER](state: State, payload:User):void {
state.user = payload;
},
[SET_PASTMOVIE](state: State, payload: SavedMovie[]):void {
state.pastMovie = payload;
},
[SET_FUTUREMOVIE](state: State, payload: SavedMovie[]): void {
state.futureMovie = payload;
},
//생략
},
actions: {
setUser({ commit }, id: number) {
commit('SET_USER', id);
},
setPastMovie({ commit }, movies: SavedMovie[]) {
commit('SET_PASTMOVIE', movies);
},
setFutureMovie({ commit }, movies: SavedMovie[]) {
commit('SET_FUTUREMOVIE', movies);
},
//생략
}
};
export default user;
3. store폴더 안에 index.ts를 만든다.
import Vuex, { createStore } from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import movie from './modules/movie';
import user from './modules/user';
import modal from "./modules/modal";
export default createStore({
modules: {
movie,
user,
modal
},
actions: {
},
plugins: [createPersistedState({
paths: ["user"]
})],
});
module을 등록해준다.
그리고 persistedstate를 쓰고 싶은 모듈은 path에 넣어준다.
4. 파일에서 접근
getters
const allCollectionList = computed(() => store.getters['movie/allCollectionList']);
state
const user = computed(() => store.state.user.user);
dispatch
store.dispatch("modal/openMakeModal", true);
728x90
'WEB > VUE.js' 카테고리의 다른 글
[VUE.js] query string (0) | 2021.12.01 |
---|---|
[VUE.js] vue persistedstate (0) | 2021.12.01 |
[VUE.js] vue-draggable-next로 drag and drop 구현 (0) | 2021.11.30 |
[VUE.js] vue3 selectbox mutiple dropdown구현하기 (0) | 2021.11.27 |
[VUE.js] vue3 intersectionObserver API 를 사용하여 infinite scroll 구현하기 (4) | 2021.11.18 |