且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SystemJS & 的相对路径ES模块导入

更新时间:2023-02-10 07:46:51

所以,这个问题是基于 Angular2 Seed (Advanced) repo found 此处.我也在那里发布了一个问题(在那里你可以看到确切的修复).长话短说:

So, this question was based around the Angular2 Seed (Advanced) repo found here. I posted an issue there as well (where you can see the exact fix). Long story short:

您需要 TypeScript 2.0 才能使用 tsconfig 文件中的 pathsbaseUrl 选项.然后在您的 SystemJS 配置文件中,您需要向 pathspackages 选项添加一些配置,如下所示:

You need TypeScript 2.0 to use the paths and baseUrl options in your tsconfig file. Then in your SystemJS config file, you need to add some configuration to the paths and packages options as follows:

packages: {
  ...
  frameworks: { defaultExtension: js, main: index.js }
  ...
},
paths: {
  ...
  frameworks: 'relative/path/to/frameworks/folder'
  ...
}

index.tsframeworks 文件夹中的一个文件,用于导出该目录中的模块.例如,如果您在 frameworks 目录中的某处有一个 language.component.ts 文件,则在 frameworks/index.ts 文件中,您将执行以下操作:

index.ts is a file INSIDE your frameworks folder which exports the modules in that directory. For example, if you had a language.component.ts file somewhere inside your frameworks directory, in the frameworks/index.ts file you would do:

export * from 'path/to/language.component';.

这允许您在您的项目中执行 import {LanguageComponent} from 'frameworks';(只要您在 frameworks 目录之外).

This allows you to do import {LanguageComponent} from 'frameworks'; in your project (as long as you are outside the frameworks directory).

希望这会有所帮助!