vue数据中的简单添加
Simple addition in vue data
我想在vue中添加两个数字
data() {
return {
details: {
num1: 100,
num2: 500,
total: num1 + num2
}
}
}
这是不可能的,也是不好的做法吗?我可以创建一个计算,但这作为临时快捷方式会很有用。难道只是范围我错了?
这是一个非常糟糕的做法!
在 Vue.js 中,您应该始终使用计算属性进行任何计算。
但在你的情况下你应该这样做:
<template>
<div>{{details.total()}}</div>
</template>
<script>
export default {
data() {
return {
details: {
num1: 100,
num2: 500,
total: () => this.details.num1 + this.details.num2
}
}
}
}
}
</script>
我想在vue中添加两个数字
data() {
return {
details: {
num1: 100,
num2: 500,
total: num1 + num2
}
}
}
这是不可能的,也是不好的做法吗?我可以创建一个计算,但这作为临时快捷方式会很有用。难道只是范围我错了?
这是一个非常糟糕的做法! 在 Vue.js 中,您应该始终使用计算属性进行任何计算。
但在你的情况下你应该这样做:
<template>
<div>{{details.total()}}</div>
</template>
<script>
export default {
data() {
return {
details: {
num1: 100,
num2: 500,
total: () => this.details.num1 + this.details.num2
}
}
}
}
}
</script>