반응형
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
반응형
'programing' 카테고리의 다른 글
Vue: setInterval 변수 값으로 클래스 변경 (0) | 2022.08.27 |
---|---|
다수의 Vue 프로젝트, 1개의 node_modules (0) | 2022.08.27 |
Control+D 신호를 캡처하는 방법 (0) | 2022.08.27 |
Java에서 안전하게 int에 캐스팅 (0) | 2022.08.27 |
memcpy(0,0,0)를 실행해도 안전한가? (0) | 2022.08.27 |