我正在使用SystemJS开发Angular 2和Typescript项目。现在,我发现我们经常不得不做这样的事情
../../../
'根据我们在给定组件中的嵌套深度从其他模块加载组件。似乎使用SystemJS的映射设置可以让我在不使用可怕的相对路径方法的情况下导入应用程序范围的模块,但我无法确定是否可以这样做。我也对解决这个问题的其他方法持开放态度。
基本上我想做的是这样的:
import {Component} from '@angular/core';
import {SecureHttpClient} from 'mySecurityModule'; //No ../../../
@Component({
moduleId: module.id,
selector: 'my-selector',
templateUrl: 'my.component.html'
})
export class MyComponent {
constructor(private client: SecureHttpClient) {
}
}
我尝试了下面的配置,但不起作用(TS编译失败)。
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
'@ng-bootstrap/ng-bootstrap': 'npm:@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
// Internal Modules
'mySecurityModule': 'app/secure/secure.module'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
我知道可能还有其他方法可以做到这一点,但我还是找不到任何明确的方法,这是我非常想解决的问题。再次感谢!