반응형
vue findIndex가 함수가 아닙니다.
여기서 에러가 발생했습니다.findIndex()
기능.
Uncaught (in promise) TypeError: state.carts.findIndex is not a function
cartId에는 rowId, qty, sizeVal 및 이미지가 있습니다.
방법들
updateCart() {
this.$store.dispatch('updateCart', this.cartId)
}
주
state: {
carts: [],
},
Vue 검사 카트
031e0275ef3746070e80d81199bd2580:Object {
id:1
name:"T-Shirt"
options:Object {
image:"products\July2018\4TXSMgf6eAdrOlaxAMiX.jpg"
}
size:"l"
price:551
qty:2
rowId:"031e0275ef3746070e80d81199bd2580"
subtotal:1102
tax:115.71
}
돌연변이
updateCart(state, rowId) {
const index = state.carts.findIndex(cart => {cart.rowId == rowId});
// console.log(index)
state.carts.splice(index, 1, {
'qty': cart.qty,
'size': cart.sizeVal,
})
},
내 액션은 일이다
행동들
updateCart(context, cart) {
return new Promise((resolve, reject) => {
axios.patch(`update/cart/${cart.id}` , {
id: cart.rowId,
qty: cart.qty,
size: cart.sizeVal,
image: cart.image
})
.then(response => {
context.commit('updateCart', response.data)
resolve(response)
})
.catch(error => {
reject(error)
})
})
},
이제 state.carts에서 카트를 업데이트하기 위해 필요한 오류는 왜 오류가 발생하는지 설명해 주십시오.
감사합니다.
당신의.state.carts
어레이가 아닌 객체입니다. findIndex
배열 방식입니다.
findIndex() 메서드는 지정된 테스트 함수를 충족하는 배열 내 첫 번째 요소의 인덱스를 반환합니다.그렇지 않으면 -1이 반환됩니다.
Vue에서 글로벌하게 가져온 혼합으로 PolyFill을 사용합니다.
const findIndex = (values, expectedValue) => {
let selectedIndex;
const valuePresent = values.some((value, index) => {
selectedIndex = index;
return value === expectedValue;
});
return valuePresent ? selectedIndex : -1;
};
findIndex는 ES6 어레이 기능입니다.브라우저에서 지원되는지 확인하거나 babel.js와 같은 polyfill 또는 transfile 플러그인을 적용해야 합니다.
언급URL : https://stackoverflow.com/questions/51277081/vue-findindex-not-a-function
반응형
'programing' 카테고리의 다른 글
Java - 현재 클래스 이름을 가져옵니다. (0) | 2022.07.19 |
---|---|
Vue nexttick을 사용하기 위해 지연된 jquery를 변경하는 방법 (0) | 2022.07.19 |
CRC32 체크섬은 어떻게 계산됩니까? (0) | 2022.07.19 |
C의 스택에 구조를 작성하려면 어떻게 해야 합니까? (0) | 2022.07.19 |
필요에 따라 필드를 표시하는 방법 확인 (0) | 2022.07.19 |