【发布时间】:2021-07-05 14:03:45
【问题描述】:
让我们看看 vue.js 3 中的以下代码段,
<template>
<div>
<h2>Computed Total - {{getPrice()}}</h2>
</div>
</template>
<script>
export default {
name: "ComputedProperties",
data(){
return{
items: [
{
id: 1,
title: 'TV',
price: 100,
},
{
id: 2,
title: 'Phone',
price: 200,
},
{
id: 3,
title: 'Laptop',
price: 300
}
]
}
},
computed: {
getPrice(){
return items.reduce((total, curr) => (total = total + curr.price), 0)
}
}
}
</script>
运行此代码后显示,error 'items' is not defined no-undef
正如我们所知,计算属性不需要声明任何参数。那么代码有什么问题呢?
【问题讨论】:
标签: vue.js vuejs3 computed-properties