programing

ES6 함수의 구문

bestcode 2022. 8. 27. 09:37
반응형

ES6 함수의 구문

가게 이름을 부르려고 하는데

methods: {
    ...mapActions('Modal', [
        'toggleActive',
    ]),
    close: () => {
        this.toggleActive();
    }

그 결과 다음과 같은 오류가 발생합니다.

Uncaught TypeError: Cannot read property 'toggleActive' of undefined

다음 작업을 수행합니다.

close: function() {
    this.toggleActive();
}

vue/vuex에서 ES6 함수 구문을 사용하려면 어떻게 해야 합니까?

화살표 기능을 사용하고 있습니다.화살표 기능이 닫힙니다.this정의되어 있는 문맥에서는, 다음과 같이 불릴 때 설정되는 것이 아니라,function기능들.예:

// Whatever `this` means here
var o = {
    foo: () => {
        // ...is what `this` means here when `foo` is called
    }
};

대신 메서드 구문을 사용하는 것이 좋습니다.

methods: {
    // (I'm using a simple method for toggleActive just for clarity; if you're
    // transpiling with something that understands rest notation for
    // objects, your `mapActions` thing is probably fine *provided* that
    // the functions it creates aren't also arrow functions
    toggleActive() {
        // ...
    },
    close() {
        ths.toggleActive();
    }
};

이것은, 통상의 모든 것에 준거하고 있는 것에 주의해 주세요.this이 질문의 답변에 기재되어 있는 내용입니다.

언급URL : https://stackoverflow.com/questions/45345784/es6-functions-syntax

반응형