반응형
상태 항목의 값을 증가시키는 방법
모든 항목을 표시하는 뷰가 있는데, 추가 버튼을 클릭하여 각 항목의 수량을 늘리고 싶다.나는 이것을 세우기 위해 문제가 있다. 이것이 내가 지금까지 생각해낸 것이다.
items: [{
id: 1,
name: "Car",
quantity: 1
},
{
id: 2,
name: "Car2",
quantity: 1
},
]
내가 투쟁하는 돌연변이 -> 여기서 배열된 특정 항목을 어떻게 표적으로 삼는가?
increment(state) {
state.items.quantity++
}
html
<div v-for="item in items" :key="item.id">
<div>
{{item.name}}
{{item.quantity}}
<button @click="add()">+</button>
</div>
html로 된 방법
add() {
this.store.commit("increment")
}
ID를 메소드에 전달하고 돌연변이에 전달:
<button @click="add(item.id)">+</button>
add(id) {
this.$store.commit("increment", id)
}
돌연변이를 준비하여 ID를 수신한 다음 수량을 찾아 증분
increment(state, id) {
const item = state.items.find(i => i.id === id);
item.quantity++;
}
참조URL: https://stackoverflow.com/questions/65586533/how-to-increment-a-value-in-state-item
반응형
'programing' 카테고리의 다른 글
Windows의 명령줄에서 Java 프로그램을 실행하는 방법 (0) | 2022.05.26 |
---|---|
printf를 사용하여 Null이 아닌 문자열을 인쇄하려면 어떻게 해야 하는가? (0) | 2022.05.26 |
렌더링된 vue에서 html 자리 표시자 특성을 이스케이프하는 방법 (0) | 2022.05.26 |
C에서 함수 호출 재정의 (0) | 2022.05.26 |
Flex/lex의 문자열 리터럴에 대한 정규식 (0) | 2022.05.26 |