因此,我在将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
安装了角度2,并试图通过 import { bootstrap } from 'angular2/platform/browser';
在我的 index.ts
中导入它
运行 tsc
时,我收到错误
src/index.ts(1,27): error TS2307: Cannot find module 'angular2/platform/browser'.
现在我想知道,我可以使用打字稿知道jspm包吗?我觉得我尝试了一切,从tsconfig排除列表中删除了jspm_packages,切换到节点模块解析或systemjs模块生成 . 也许我还没找到合适的组合 . 关于下一步尝试的任何提示?
1 回答
我也在努力解决同样的问题,经过一些研究,我了解到目前还没有很好的解决方案 .
解决方法:
A)您可以复制依赖项并使用npm进行安装 . 自从从npm解析后,tsc不应该抛出任何错误 .
B)更改tsconfig.json并将angular映射到jspm路径 . 例如
有关完整示例,请参阅https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2 .
请注意,“baseUrl”和“paths”不是官方属性,仅适用于typescript编译器的beta版本 .
目前正在跟踪此问题:https://github.com/Microsoft/TypeScript/issues/6012