我将 systemjs.config.js 中的库导入为:
map: {
...
'firebase': 'npm:firebase',
'angularfire2': 'npm:angularfire2/bundles/angularfire2.umd.js',
'angularfire2/auth': 'npm:angularfire2/auth',
'angularfire2/database': 'npm:angularfire2/database',
...
},
packages: {
app: {
defaultExtension: 'js',
meta: {
'./*.js': {
loader: 'systemjs-angular-loader.js'
}
}
},
'firebase':{
main: './firebase.js',
defaultExtension: 'js'
},
'angularfire2/database':{
main: './database.js',
defaultExtension: 'js'
},
'angularfire2/auth':{
main: './auth.js',
defaultExtension: 'js'
},
}
错误屏幕上限
更新
我只是想强调这个错误
无法加载转换器以转换http://localhost:3000/node_modules/angularfire2/database/database.js 加载http://localhost:3000/node_modules/angularfire2/database/database.js时出错" angularfire2 / database"来自http://localhost:3000/app/app.module.js
答案 0 :(得分:0)
好的,因为角度开发人员不断改变他们的设置,这里是当前的逐步说明,如何使用angular 4 quickstart示例加载angularfire2数据库模块。
从克隆官方快速启动回购开始
git clone https://github.com/angular/quickstart.git quickstart
安装其他依赖项
npm i --save-dev angularfire2 firebase promise-polyfill plugin-typescript@6.0.4
将tsconfig.json更改为使用"module": "system"
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
向systemjs.config.js添加必要的东西
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
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',
'angularfire2': 'npm:angularfire2',
'ts': 'npm:plugin-typescript',
'typescript': 'npm:typescript/lib/typescript.js',
'firebase': 'npm:firebase',
'promise-polyfill': 'npm:promise-polyfill/promise.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts',
meta: {
'./*.ts': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
},
angularfire2: {
main: 'angularfire2.js',
defaultExtension: 'js'
},
firebase: {
defaultExtension: 'js'
},
ts: {
main: 'lib/plugin.js',
defaultExtension: 'js'
}
}
});
})(this);
将AngularFireModule
和AngularFireDatabaseModule
添加到app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp({
// NOTE: USE YOUR OWN API KEY
apiKey: "AIzaSyDFbuKX0UeXje-PRAvwIymYo2jk-uGqMa4",
authDomain: "test-bc800.firebaseapp.com",
databaseURL: "https://test-bc800.firebaseio.com",
storageBucket: ""
}),
AngularFireDatabaseModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
在db
app.component.ts
import { Component } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Subject } from 'rxjs/Subject';
@Component({
selector: 'my-app',
template: '<h1>Angularfire2 example app</h1>'
})
export class AppComponent {
constructor(db: AngularFireDatabase) {
// use db here
}
}
请注意,虽然此示例有效,但它会将浏览器中的角度源代码从es6编译为javascript。似乎预先捆绑的angularfire代码没有与npm i angularfire2
一起安装,因此您必须自己捆绑它以进行生产。