【问题标题】:Import a module without its path in angular 6在角度 6 中导入没有路径的模块
【发布时间】:2018-10-08 10:00:57
【问题描述】:

我有一个名为 ng2-toastr 的第三方模块,我将它移到我的一个应用程序文件夹中,并将其从节点模块中删除,并使用 npm uninstall ng2-toastr。现在我想在我的 AppModule 和其他模块中按它的名称(就像我以前在 node_modules 中所做的那样)而不是按它的路径导入它,我该怎么做?

【问题讨论】:

  • 不是 100% 确定,所以添加为评论... 在 angular.json 中,有一个“脚本”属性(项目 > 根 > 架构师 > 构建 > 脚本)属性。我认为您为此添加了路径。请告诉我是否正确。
  • 那样不行

标签: javascript angular typescript


【解决方案1】:

您可能想详细阅读此内容http://www.typescriptlang.org/docs/handbook/module-resolution.html

话虽如此,您可以通过向您的 tsconfig.json 添加路径来实现该文档中的说明。

{
  "compilerOptions": {
    "baseUrl": ".", // This must be specified if "paths" is.
    "paths": {
      "yourmodule": ["path/to/yourmodule"] // This mapping is relative to "baseUrl"
    }
  }
}

注意 node_modules 中的东西有一个默认的树遍历方法来做你想做的事情,这就是为什么当你在 node_modules 中使用它时它会起作用(也在该文档中详细解释了)。

【讨论】:

  • 它解决了问题并且应用程序运行了,但是 IDE (WebStorm) 仍然无法引用该库。
  • 啊,这可能是对 WebStorm 的限制,请从他们的支持中阅读:intellij-support.jetbrains.com/hc/en-us/community/posts/… 本质上,模块可能需要在 PATH 上。希望对一些人有所帮助,我并没有真正使用 WebStorm。
猜你喜欢
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2019-12-05
  • 1970-01-01
  • 2016-12-19
  • 2018-08-26
  • 1970-01-01
相关资源
最近更新 更多