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

andcl:

我有一个普通的单个文件组件,该组件同时具有计算属性和一些方法

<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>

当我尝试this.formattedMatches() 从该方法访问时,出现错误消息[Vue warn]: Error in render: "TypeError: this.formattedMatches is not a function"

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

莫里兹物质:

您可以访问计算的属性,例如property,而不是method

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

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

注意:如果您将其作为带穿刺术的方法来处理,()则会抛出类似this的错误Error in v-on handler: "TypeError: this.myProperty is not a function"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章