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'
})
}
添加回答
举报