【发布时间】:2019-03-01 10:59:20
【问题描述】:
也许很明显,但我有一个 Vue 单文件组件,如下所示:
<template>
...
</template>
<script>
export default {
props: [ 'matches', 'results' ],
computed: {
formattedMatches: function () {
let rows = [];
this.matches.forEach(function($match, $i) {
rows[$i] = {
id: $i + 1,
title: $match[0] + ' ' + $match[1]
};
});
return rows;
},
formattedResults: function () {
let rows = [];
this.results.forEach(function($resultRow, $i) {
rows[$i] = {
id: $i + 1,
title: this.formattedMatches[$i]
// Error in render: "TypeError: Cannot read property 'formattedMatches' of undefined"
};
});
return rows;
},
...
</script>
如果我尝试使用this.matches,也会出现错误,而不仅仅是this.formattedMatches。我想这是类和方法中变量范围的问题,但我什至不知道是否有另一种更好的方法或模式来实现相同的行为。
有什么想法吗?提前致谢。
【问题讨论】:
标签: vue.js vuejs2 computed-properties