programing

vue에서 getters로 파라미터를 송신하는 방법

javaba 2022. 8. 27. 21:58
반응형

vue에서 getters로 파라미터를 송신하는 방법

카트의 재고와 수량을 확인할 수 있는 게터가 있습니다.여기 나의 getters.js:

export const checkStock = (state, productID) => {
    let stockAvailable = true;
    state.cart.forEach(item => {
        if(item.product.id == productID) {
            if(item.product.attributes.stock <= item.amount){
                stockAvailable = false;
            }
        }
    })
    return stockAvailable;
}

그래서 보다시피, 저는productID이 함수에 파라미터로 지정합니다.그리고 Product 컴포넌트에서 이 함수를 호출하고 추가하려고 합니다.productID이 기능을 하는데 노하우를 모르겠어요.

checkStockAvailability(productId) {
            return this.$store.getters.checkStock;
        },

그래서 내가 어떻게 더 할 수 있을 것 같아?productID어떻게 해야 할까요?

결과를 반환하는 함수를 반환하여 getter에서 파라미터를 사용할 수 있습니다.

export const checkStock = (state) => (productId) => {
    // call the checkStockAvailability here
    let stockAvailable = true;
    state.cart.forEach(item => {
        if(item.product.id == productID) {
            if(item.product.attributes.stock <= item.amount){
                stockAvailable = false;
            }
        }
    })
    return stockAvailable;
}

// then you can use this getter on the component as

const productId = 2;
store.getters.checkStock(productId)

언급URL : https://stackoverflow.com/questions/69630669/how-to-send-a-parameter-to-getters-in-vue

반응형