亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何跨突變將新附加值添加到狀態?

如何跨突變將新附加值添加到狀態?

米琪卡哇伊 2022-10-08 15:05:22
我有這個代碼:export default new Vuex.Store({    state: {     bottles: 20,     disabledBtn: false    },mutations: {REMOVE_BOTTLES(state) {  if (state.bottles > 0) {    state.bottles--;  }},ADD_BOTTLES(state, items) {  state.bottles = state.bottles + items  }   },actions: {removeOneBottle ({commit}) {  commit('REMOVE_BOTTLES');},addBottlesInput ({commit}, items) {  commit('ADD_BOTTLES', items);  } }})我需要將新添加的值添加到狀態中。在突變中,它只是作為字符串添加,但我需要它來添加通過輸入傳遞的數字。我將不勝感激。
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

假設您的問題是items字符串而不是數字,請嘗試將您的ADD_BOTTLES突變更改為


ADD_BOTTLES (state, items) {

  let num = parseInt(items, 10)

  if (!isNaN(num)) {

    state.bottles += items

  }

}

如果您通過表單從用戶那里獲取值,請考慮使用.number修飾符。例如(根據您的屏幕截圖)...


<template>

  <section>

    <p>How many bottles are there in total:</p>

    <p>{{ bottles }}</p>

  </section>

  <section>

    <p>How many bottles of water were brought?</p>

    <input type="number" v-model.number="items" min="0">

    <button @click="addBottles(items)">Add</button>

  </section>

</template>

import { mapState, mapMutations } from 'vuex'


export default {

  data: () => ({ items: 0 }),

  computed: mapState(['bottles']),

  methods: mapMutations({

    addBottles: 'ADD_BOTTLES'

  })

}



查看完整回答
反對 回復 2022-10-08
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號