gpt4 book ai didi

javascript - 从 Vue.js 2 中的计算属性推送到数组

转载 作者:搜寻专家 更新时间:2023-10-30 22:39:10 25 4
gpt4 key购买 nike

我希望从 Vue.js 2 中的计算属性中推送到一个数组,Vue 正在 Laravel 中使用,我收到以下响应。

createSelection:"(error during evaluation)"

正在使用以下代码:

<template>
<div>
<div>Credits carried through: {{ credits }}</div>
<div v-for="meal in meals">
{{meal}}
<input :id="meal" :name="meal" v-model.number="creditsPerMeal[meal]" type="number">
</div>
<div>
Credits used: {{creditsSum}}/{{credits}}
</div>
</div>
</template>

<script>
export default {

mounted() {
console.log('Component ready.');

console.log(JSON.parse(this.f));

},

props: ['f','c'],

name: 'credits',
data: function () {
var meals = JSON.parse(this.f)

var creditsPerMeal = {}
for (var i = 0; i < meals.length; i++) {
creditsPerMeal[meals[i]] = 0
}

var createSelection = []


return {
credits: this.c,
meals,
creditsPerMeal
}
},

computed: {
creditsSum () {
return Object.values(this.creditsPerMeal).reduce((a, b) => a + b, 0)
},

createSelection: function (){
for (var i = 0; i < meals.length; i++) {
createSelection.push({
food: meals[i],
quantity: creditsPerMeal[meals[i]]
})
}
}
}
}
</script>

最佳答案

计算方法应该返回一些东西,实际上他们不应该做任何事情,只是计算一些东西然后返回。您的计算方法根本没有返回。首先将您的推送逻辑移至方法:

   computed: {
creditsSum () {
return Object.values(this.creditsPerMeal).reduce((a, b) => a + b, 0)
},
},
methods: {
createSelection (){
for (var i = 0; i < meals.length; i++) {
createSelection.push({
food: meals[i],
quantity: creditsPerMeal[meals[i]]
})
}
}
}

此外,error during evaluation 不是问题的描述,看起来不像 Vue 问题,也许您可​​以提供更详细的错误?

关于javascript - 从 Vue.js 2 中的计算属性推送到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com