从离子1过渡到离子2,并好奇如何使用它们的类型记录示例设置类似于firebase import * as Firebase from 'somewhere/foo/';的内容。
这是火基教程https://www.firebase.com/docs/web/libraries/ionic/guide.html中的旧代码
index.html
<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/1.2.0/angularfire.min.js"></script>app.js
angular.module("starter", ["ionic", "firebase"])其中只包含对Firebase库的cdn引用。我们如何在离子2和类型记录中做到这一点?
发布于 2016-04-11 00:02:30
ionic2应用程序中没有引导程序..。
angularfire2和firebase的npm模块。app.ts
import 'es6-shim';
import {App, Platform} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HomePage} from './pages/home/home';
import {FIREBASE_PROVIDERS, defaultFirebase, AngularFire} from 'angularfire2';
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
providers: [
FIREBASE_PROVIDERS,
defaultFirebase('https://[YOUR-APP].firebaseio.com/')
],
config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
export class MyApp {
rootPage: any = HomePage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
}
}home.ts
import {Page} from 'ionic-angular';
import {Component} from 'angular2/core';
import {AngularFire} from 'angularfire2';
import {Observable} from 'rxjs/Observable';
@Page({
template: `
<ion-navbar *navbar>
<ion-title>
Home
</ion-title>
</ion-navbar>
<ion-content class="home">
<ion-card *ngFor="#item of bookItems | async">
<ion-card-header>
{{item.volumeInfo.title}}
</ion-card-header>
<ion-card-content>
{{item.volumeInfo.description}}
</ion-card-content>
</ion-card>
</ion-content>`
})
export class HomePage {
bookItems: Observable<any[]>;
constructor(af: AngularFire) {
this.bookItems = af.list('/bookItems');
}
}git repo - aaronksaunders/离子2-角火-样品中的完整源
您可以侦听类似于以下的身份验证事件
ngOnInit() {
// subscribe to the auth object to check for the login status
// of the user, if logged in, save some user information and
// execute the firebase query...
// .. otherwise
// show the login modal page
this.auth.subscribe((data) => {
console.log("in auth subscribe", data)
if (data) {
this.authInfo = data.password
this.bookItems = this.af.list('/bookItems');
} else {
this.authInfo = null
this.displayLoginModal()
}
})
}发布于 2016-04-10 14:38:02
您需要将Firebase和Angularfire2配置到SystemJS配置中:
System.config({
map: {
firebase: '/node_modules/firebase/lib/firebase-web.js',
angularfire2: ' node_modules/angularfire2'
},
packages: {
angularfire2: {
main: 'angularfire2.js',
defaultExtension: 'js'
},app: {
format: 'register',
defaultExtension: 'js'
}
},
});这样,您将能够AngularFire2。
然后,首先是在引导应用程序时指定Angularfire2提供程序:
(...)
import {FIREBASE_PROVIDERS, defaultFirebase, AngularFire} from 'angularfire2';
bootstrap(AppComponent, [
FIREBASE_PROVIDERS,
defaultFirebase('https://<your-firebase>.firebaseio.com'),
(...)
]);然后可以注入AngularFire类:
(...)
import {AngularFire} from 'angularfire2';
@Component({
(...)
})
export class AppComponent {
constructor(private af: AngularFire) {
}
}https://stackoverflow.com/questions/36530765
复制相似问题