在學習angular2教程時,裏面的“英雄編輯器”實例項目在模擬服務(內存 Web API)中獲取和保存數據時用到了In-Memory Web API,那麼怎麼將In-Memory Web API添加到systemjs加載器中呢?
1.打開package.json,添加依賴包:
"angular-in-memory-web-api": "^0.3.1",
"systemjs": "0.19.40",
2.通過命令行窗口,執行命令來添加本項目的systemjs包
$ cd AngularDemo3
$ npm install
依賴添加完後,在生成的node_modules文件夾下就多出了一個systemjs的依賴包
3.在src目錄下添加systemjs.config.js文件,內容如下,爲模塊加載器提供了該到哪裏查找應用模塊的信息,並註冊了所有必備的依賴包。
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(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',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// 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);
4.按照“英雄編輯器”實例教程在app.module.ts中導入相應模塊使用即可。