2017-12-31 119 views
0

reactjs 동적 가져 오기를 사용하는 동안 이상한 문제가 발생했습니다. 예를 들어 이름이 ComponentA이고 경로가 myComponents/ComponentA 인 컴포넌트가 있다고 가정 해 보겠습니다. 나는 동적으로 다음 코드처럼 가져올 때 지금, 그것은 잘 작동 :reactjs constant path 변수로 동적 가져 오기

//before definition of my current component 
const PATH = 'myComponents/ComponentA'; 
. 
. 
. 
// some where in my component class 
Promise.all(
    [ 
     import(PATH), 
     // other imports 
    ] 
).then(....); 

이 나에게 줄 것 인 :

Promise.all(
     [ 
      import('myComponents/ComponentA'), 
      // other imports 
     ] 
    ).then(....); 

을하지만 다음과 같이 일정한 변수에 내 구성 요소 경로를 정의하는 경우 이 같은 오류 :

Error: Cannot find module 'myComponents/ComponentA'.

그리고 난 그냥 문제와 그렇지 않은 몇 번을 해결할 내 PATH 변수에 빈 문자열을 추가하면 몇 번.

//before definition of my current component 
const PATH = 'myComponents/ComponentA'; 
. 
. 
. 
// some where in my component class 
Promise.all(
    [ 
     import(''+PATH), // some times by adding empty string, problem would be solved 
     // other imports 
    ] 
).then(....); 

무슨 일이 일어나고 있는지에 대한 아이디어는 인정 될 것입니다.

답변

0

maybe try this , new es6 syntax...

const PATH = 'myComponents/ComponentA'; 
Promise.all(
    [ 
     import(`${PATH}`), // try pass it like this 
     // other imports 
    ] 
).then(....); 

는 내가 도움

+0

내열왔다 바랍니다. 당신의 반응을위한 탱크,하지만 작동하지 않았다. – pooyan

+0

백틱을 사용해 보셨습니까? 우리는이'import ('$ {PATH}')'와 같은 공간으로 시도 할 수 있습니다. ($ {PATH}가 아닌 주석은 주석에서 이것을 할 수 있습니다.) –

+0

늦은 응답으로 미안하지만 너무 효과적이지 않습니다. – pooyan