2017-09-22 1 views
-1

아래 예제는 "hello world"를 출력하지 않는 이유는 무엇입니까? 대신에, 나는 점점 오전 :자바 스크립트 내보내기/가져 오기 클래스

TypeError: _base2.default.test is not a function

(가 바벨로 transpiled되고있다)

file1.js

import Example from './file2'; 
console.log(Example.test()); 

file2.js

export default class Example { 
    test() { 
    console.log('hello world'); 
    } 
} 

답변

1

당신 클래스 만 가져 오지만 n 구약 당신은 정적 인 방법을 시도 할 수 있습니다 (개체 인스턴스를 생성하지 않고) 클래스 메소드와 같은 메소드를 호출하려면 클래스

의 인스턴스가

var myInstance = new Example() 
myInstance.test() 
1

을 시도하고.

당신은 다음의 file2.js에게

export default class Example { 
    static test() { 
    console.log('hello world'); 
    } 
} 

을 같은을 변경 통화 할 경우

import Example from './file2'; 
console.log(Example.test()); 

James Maa 대답을 참조하십시오

file1.js에 CALSS 이름을 사용하여 호출 할 수 있습니다 그것을 인스턴스 메서드로.