source

Vue.js를 사용하여 단일 파일 구성 요소의 메서드에서 계산된 속성에 액세스하는 방법

goodcode 2022. 8. 14. 11:58
반응형

Vue.js를 사용하여 단일 파일 구성 요소의 메서드에서 계산된 속성에 액세스하는 방법

계산된 속성과 가지 메서드를 모두 가진 일반적인 단일 파일 구성 요소가 있습니다.

<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());

주의: 파라세시스를 사용하는 방법으로 취급하는 경우()그것은 다음과 같은 오류를 던질 것이다.Error in v-on handler: "TypeError: this.myProperty is not a function".

언급URL : https://stackoverflow.com/questions/52411711/how-to-access-a-computed-property-from-a-method-in-a-single-file-component-with

반응형