代码之家  ›  专栏  ›  技术社区  ›  Gowthaman

在导入该组件之前,有没有办法检查该组件是否存在

  •  0
  • Gowthaman  · 技术社区  · 7 年前

    我有一个场景,我想加载一个组件,并在该组件的文件存在时渲染它,如果不返回另一个组件的话。

    if('./Shared/MyComponent') exists then 
    Import MyCompoent from './Shared/MyCompoent'
    Else
    Import MyCompoent from './Shared/MyNewCompoent'
    

    有没有办法做到这一点

    1 回复  |  直到 7 年前
        1
  •  -2
  •   gbalduzzi    7 年前

    嗯,这有点老套,可能还有更完善的解决方案,但这个 应该 工作(目前无法测试)

    try {
     import MyComponent from './component'
    } catch (e) {
     import MyComponent from './new-component'
    }
    
    推荐文章