2017-12-31 54 views
0

reactjs動的インポートを使用しているときに奇妙な問題に直面しました。たとえば、名前がComponentAで、そのパスがmyComponents/ComponentAのようなコンポーネントがあるとします。今、私は動的に次のコードのようにそれをインポートするとき、それはうまく動作します:reactjs定数パス変数を使用した動的インポート

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

しかし、私は次のように一定の変数での私のコンポーネントのパス定義した場合:

//before definition of my current component 
const PATH = 'myComponents/ComponentA'; 
. 
. 
. 
// some where in my component class 
Promise.all(
    [ 
     import(PATH), 
     // 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

のHIとなっている願っています。あなたの反応のためのタンクが、動作しませんでした。 – pooyan

+0

あなたはバックティックを試しましたか?私たちはこの 'import( '$ {PATH}')'のようなスペースで試してみることができます($ {PATH}はバックスティックでコメントできません) –

+0

遅刻して申し訳ありませんが、これもうまくいきませんでした。 – pooyan

関連する問題