更改另一页时数量值不变

时间:2019-07-10 16:17:05

标签: vue.js vuex

当在vuex中更改另一个页面时,数量值不会更改。

我有以下问题: 1.输入的数量值为1:产品1页面中的value = 3。 2.没有点击提交。 3.更改页面产品2。 4.value不重置。 请帮我解决这个问题!谢谢!!

Store.js '''

let cart = window.localStorage.getItem('cart');
let cartCount = window.localStorage.getItem('cartCount');
export default new Vuex.Store({
    //strict: true,
    // isCheckout: false,
    state: {
        isLoading: false,
        purchase : {
            number: 1,
        },
        // items:  {
        //  id: 3,
        //  name: 'Huawei P30',
        //  url: 'https://www-file.huawei.com/-/media/corporate/images/home/small-banner/2019/p30_box1.png?la=en',
        //  price: '200',
        //  description: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. '
        // },  
        cart: cart ? JSON.parse(cart) : [],
        cartCount: cartCount ? parseInt(cartCount) : 0,
    },
    getters: {
        isLoading: state => state.isLoading,
        purchase: state => state.purchase,
        //state.number = { ...state.number, 1}
        items: state => state.items,
        // items: state => {
        //  return state.items.map(({ id, quantity }) => {
        //      const product = state.products.find(product => product.id === id)
        //      return {
        //          title: product.title,
        //          price: product.price,
        //          quantity
        //      }
        //  })
        // },
    },
    mutations: {
        changeQuantity (state, number) {
            state.purchase.number = number
        },
        [types.ADD_TO_CART](state, item, purchase) {
            let found = state.cart.find(product => product.id === item.id);
            let number = parseFloat(state.purchase.number);
            console.log(number);
            if (found) {
                found.quantity += number;
                //console.log(found.quantity)
                found.totalPrice = found.quantity * found.price;
            } else {
                state.cart.push(item);
                Vue.set(item, 'quantity', number);
                Vue.set(item, 'totalPrice', number*(item.price));
            };
            state.cartCount += number;
            state.purchase.number = 1;
            this.commit('saveCart');
        },

''' 的HTML '''

 <dd class="col-sm-10"> 
                      <input class="quantity" @input='changeQuantity' type="number" name="" min="1" :value='purchase.number' placeholder="1">
                    </dd>

''' Vuejs

computed: {
  ...mapGetters ([
    'isLoading','purchase',
    ]),
},

0 个答案:

没有答案