React:如何使用 if 语句动态导入?

React: How do I import dynamically using if statements?

我正在尝试了解如何根据应用程序的当前语言导入我的网站图标。我遵循了@thecodejack 的回答,但它仍然 returns 一个错误:

'faviconApple' 未定义
'favicon32x32' 未定义
...
我做错了什么?

我试过的

  if(this.state.language === 'nl') {
  import ('./library/imgs/favicon/nl/apple-touch-icon.png')
  .then((faviconApple)) 
  import ('./library/imgs/favicon/nl/favicon-32x32.png')
  .then((favicon32x32)) 
  import ('./library/imgs/favicon/nl/favicon-16x16.png')
  .then((favicon16x16)) 
  import ('./library/imgs/favicon/nl/safari-pinned-tab.svg')
  .then((faviconSafari)) 
  import ('./library/imgs/favicon/nl/favicon.ico')
  .then((favicon)) 
} else if(this.state.language === 'fr') {
  import ('./library/imgs/favicon/fr/apple-touch-icon.png')
  .then((faviconApple)) 
  import ('./library/imgs/favicon/fr/favicon-32x32.png')
  .then((favicon32x32)) 
  import ('./library/imgs/favicon/fr/favicon-16x16.png')
      .then((favicon16x16)) 
  import ('./library/imgs/favicon/fr/safari-pinned-tab.svg')
      .then((faviconSafari)) 
  import ('./library/imgs/favicon/fr/browserconfig.xml')
.then((faviconBrowserconfig)) 
} else if(this.state.language === 'en') {
  import ('./library/imgs/favicon/en/apple-touch-icon.png')
      .then((faviconApple)) 
  import ('./library/imgs/favicon/en/favicon-32x32.png')
      .then((favicon32x32)) 
  import ('./library/imgs/favicon/en/favicon-16x16.png')
      .then((favicon16x16)) 
  import ('./library/imgs/favicon/en/safari-pinned-tab.svg')
      .then((faviconSafari)) 
  import ('./library/imgs/favicon/en/favicon.ico')
      .then((favicon)) 
}

在您的代码中,您没有将已解析的模块分配给任何东西。这可能是问题所在。 尝试

let faviconApple;
if (this.state.language === 'nl') {
  import('./library/imgs/favicon/nl/apple-touch-icon.png')
    .then(favA => { faviconApple = favA; });
}