【问题标题】:typescript: import jspm libraries打字稿:导入 jspm 库
【发布时间】:2016-06-17 03:54:56
【问题描述】:

所以我在将 jspm 包导入到 typescript 时发现的大多数示例都假设我想使用 Systemjs 在浏览器中加载和解释它们。但是,我更愿意使用tsc 来构建commonjs 模块并只导入js 代码,因为在我看来,这对我来说是更通用且抗错误的方法。

所以我的目录结构是这样的:

src/index.ts
jspm_packages/...
config.js
tsconfig.json

使用 tsconfig 具有以下内容:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "rootDir": "src",
    "outDir": "target/app",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true
  },
  "exclude": [
    "jspm_packages",
    "node_modules",
    "typings",
    "target"
  ]
}

出于测试目的,我使用jspm install npm:angular2 安装了angular 2,并尝试通过import { bootstrap } from 'angular2/platform/browser'; 将其导入我的index.ts

运行tsc 时出现错误

src/index.ts(1,27): error TS2307: Cannot find module 'angular2/platform/browser'.

现在我想知道,我可以让 typescript 知道 jspm-packages 吗?我觉得我尝试了一切,从 tsconfig 排除列表中删除 jspm_packages,切换到节点模块解析或 systemjs 模块生成。也许我只是没有找到合适的组合。关于下一步要尝试什么的任何提示?

【问题讨论】:

  • jspm 网站的第一行是“jspm 是 SystemJS 通用模块加载器的包管理器,构建在动态 ES6 模块加载器之上”。我不认为在没有 system.js 的情况下使用 jspm 是正确的方法。

标签: angularjs typescript systemjs jspm


【解决方案1】:

我也在为同样的问题而苦苦挣扎,经过一些研究,我了解到目前还没有好的解决方案可以解决这个问题。

解决方法:

A) 您可以复制您的依赖项并使用 npm 安装它。 tsc 不应该抛出任何错误,因为它是从 npm 解析的。

B) 更改您的 tsconfig.json 并将 angular 映射到 jspm 路径。例如

"baseUrl": ".",
"paths": {
     "angular2/*": [
        "jspm_packages/npm/angular2@2.0.0-beta.7/*"
     ],
     "rxjs/*": [
        "jspm_packages/npm/rxjs@5.0.0-beta.2/*"
     ]
  }

有关完整示例,请参阅 https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2

请注意,“baseUrl”和“paths”不是官方属性,仅适用于 typescript 编译器的 beta 版本。

目前正在此处跟踪该问题: https://github.com/Microsoft/TypeScript/issues/6012

【讨论】:

  • 我觉得这太难了,因为 intellij 还不能识别路径选项。我反其道而行之,将 jspm 指向 node_modules
猜你喜欢
  • 2020-04-19
  • 2023-02-09
  • 2012-12-26
  • 1970-01-01
  • 2016-09-23
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多