Vuejs2 如何使用Vue.js从单个文件组件中的方法访问计算属性

Vuejs2 如何使用Vue.js从单个文件组件中的方法访问计算属性,vuejs2,vue-component,computed-properties,Vuejs2,Vue Component,Computed Properties,我有一个普通的单文件组件,它既有计算属性,也有一些方法: <template>...</template> <script> ... export default { props: ['matches'], data: function() {...} // No problem with these computed: { formattedMatches: function () { le

我有一个普通的单文件组件,它既有计算属性,也有一些方法

<template>...</template>
<script>
...
export default {
    props: ['matches'],
    data: function() {...}  // No problem with these

    computed: {
        formattedMatches: function () {
            let formatted = [];
            this.matches.forEach(function($match, $i, $arr) {
                formatted[$i] = $match[0];
            };
        });
        return formatted;
    }
    ...

    methods: {
        getData: function() {
            return this.formattedMatches();
        },
        ...
    }
}
<script>
。。。
...
导出默认值{
道具:['matches'],
数据:function(){…}//这些函数没有问题
计算:{
formattedMatches:函数(){
设格式化=[];
this.matches.forEach(函数($match,$i,$arr){
格式化的[$i]=$match[0];
};
});
返回格式;
}
...
方法:{
getData:function(){
返回此.formattedMatches();
},
...
}
}
当我尝试从方法访问
this.formattedMatches()
时,我在呈现中得到一个
[Vue warn]:错误:“TypeError:this.formattedMatches不是函数”

实现我想要的东西的正确方法是什么?
提前感谢。

您可以访问计算属性,如
属性
,而不是
方法

// correct    
console.log(this.myProperty);

// wrong    
console.log(this.myProperty());

注意:如果您将其视为具有穿刺的方法
()
它将在v-on处理程序中抛出类似的
错误:“TypeError:this.myProperty不是函数”

计算属性是一个属性,而不是一个方法,因此将
this.formattedMatches()
更改为
this.formattedMatches
,谢谢,这就解决了问题,你是对的。如何知道just属性是必需的,总是作为方法尝试,谢谢