如何使用离子2 /角度2和打字稿设置firebase

Mon*_*key 6 typescript ionic2 angular

从离子1过渡到离子2,并且很好奇如何import * as Firebase from 'somewhere/foo/';使用他们的打字稿示例设置像firebase这样的东西.

  1. bower是在离子2中安装js依赖项的标准方法,还是应该使用其他一些构建链/工具来添加像Firebase这样的东西?

  2. 我应该使用bower install来安装firebase库还是应该直接指向firebase cdn脚本源?

  3. 我应该使用typings来安装firebase打字稿定义吗?

这是firebase教程中的旧代码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>
Run Code Online (Sandbox Code Playgroud)

app.js

angular.module("starter", ["ionic", "firebase"])
Run Code Online (Sandbox Code Playgroud)

其中只包含对Firebase库的cdn引用.我们如何在离子2和打字稿中做到这一点

Aar*_*ers 4

ionic2 应用程序中没有引导程序...

  • 您可以加载 npm 模块angularfire2firebase
  • 在应用程序组件上设置提供程序
  • 指定您的应用程序 URL

应用程序.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();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

主页.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');
    }
}
Run Code Online (Sandbox Code Playgroud)

git repo 中的完整源代码 - aaronksaunders/ionic2-angularfire-sample

您可以监听这样的身份验证事件

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()
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

请参阅此处的代码