为了便于解释,我在.vue单个文件中做了一个代码示例
<template>
<div id="app">
<h1>{{score}} : Score with undefined minScore but declared on data</h1>
<h1>{{score2}} : Score with undeclared/undefined minScore2</h1>
</div>
</template>
<script>
export default {
data() {
return {
minScore: undefined,
// Uncomment minScore2 to fix weird behavior
// minScore2: undefined
}
},
computed: {
score() {
return this.minScore + 5
},
score2() {
return this.minScore2 + 5
}
},
created() {
setTimeout(() => {
this.minScore = 10
this.minScore2 = 10
}, 1000)
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
我们使用上面的组件将分数显示为 …