티스토리 뷰
Vuex 기술 요소
- state : 여러 컴포넌트에 공유되는 데이터 data
- getters : 연산된 state 값을 접근하는 속성 computed
- mutaions: state 값을 변경하는 이벤트 로직 메서드 methods
- actions : 비동기 처리 로직을 선언하는 메서드 aysnc methods
뭐가 다른지 싶은데 helper 함수를 쓰게 되면 편하게 쓰게 된다.
import { createStore } from "vuex";
export const store = createStore({
state: {
headerText : 'ToDo!'
}
})
스토어에서 이렇게 작성했고
<template>
<div>
<header>
<h1>{{ this.$store.state.headerText }}</h1>
</header>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
h1{
color:#2F3B52;
font-weight: 900;
margin : 2.5rem 0 1.5rem;
}
</style>
this.$store.state.headerText를 이용해서 접근하면 콘솔에서도 확인이 가능하다.
제대로 코딩을 시작하겠다.
import { createStore } from "vuex";
const storage = {
fetch(){
const arr = [];
if(localStorage.length > 0){
for(var i = 0; i < localStorage.length; i++){
arr.push(JSON.parse(localStorage.getItem(localStorage.key(i))));
}
}
return arr;
}
}
export const store = createStore({
state: {
todoItems : storage.fetch()
}
})
이렇게 store.js에 const storage 에 fetch()함수로 담았다. fetch는 그냥 작명한거다.
로컬스토레이지에 있던 값을 꺼내서 todoItems에 넣어줬다. 그것뿐이다.
실무에서는 로컬스토레이지가 아닌 디비데이터를 가져와서 뿌려주는 역할을 할 것 같다.
<template>
<div>
<TransitionGroup name="list" tag="ul">
<li v-for="(todoItem,i) in this.$store.state.todoItems" v-bind:key="i"
class="shadow">{{ todoItem.item }}
<button v-on:click="removeTodo(todoItem, i)" class="removeBtn">
<i class="removeBtn fas fa-trash-alt"></i>
</button>
<button v-on:click="toggleClear(todoItem,i)">clear</button>
<span v-if="todoItem.completed">good!</span>
</li>
</TransitionGroup>
</div>
</template>
todoList.js 에서 v-for="(todoItem,i) in this.$store.state.todoItems" 로 반복문을 이용해 뿌려줄 수 있었다.
'dev_공부일지 > vue.js' 카테고리의 다른 글
Vuex actions 비동기 처리 로직을 선언하는 메소드! (0) | 2024.05.11 |
---|---|
state는 왜 직접 변경 안하고 mutations를 변경할까? (0) | 2024.05.11 |
Vuex 설치 및 등록 (0) | 2024.05.10 |
Vuex가 필요한 이유, 컨셉, 구조 (0) | 2024.05.10 |
vuex 소개 - 상태 관리 라이브러리 (0) | 2024.05.10 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- react실행
- 컨트
- 로그인
- hypertexttransferprotocol
- 향해플러스백엔드
- 항해99
- thymleaf
- 백엔드 개발자 공부
- 인터셉터
- rejectValue
- React
- 스프링부트
- Java
- filter
- 리터럴
- HTTP
- reject
- 스프링공부
- JPA
- 향해99
- exception
- Intercepter
- ArgumentResolver
- 향해플러스
- 백엔드 개발자 역량
- SpringBoot
- 항해플러스
- BindingResult
- 예외처리
- jpa api
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함