如何使用 ionic 2/angular 2 和 typescript 设置 firebase [英] how to set up firebase with ionic 2 / angular 2 and typescript

查看:13
本文介绍了如何使用 ionic 2/angular 2 和 typescript 设置 firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 ionic 1 过渡到 ionic 2,并且很好奇如何使用他们的 typescript 示例设置诸如 firebase import * as Firebase from 'somewhere/foo/'; 之类的东西.

Making transition from ionic 1 to ionic 2 and was curious about how to set up something like firebase import * as Firebase from 'somewhere/foo/'; using their typescript example.

  1. Bower 是在 ionic 中安装 js 依赖项的标准方式吗2 或者我应该使用其他构建链/工具来添加Firebase 之类的?

  1. Is bower the standard way of installing js dependencies in in ionic 2 or should I be using some other build chain/tool for adding something like Firebase?

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

Should I use bower install to install the firebase libraries or should point directly to a firebase cdn script source?

我应该使用类型来安装 Firebase 打字稿定义吗?

Should I using typings to install firebase typescript definitions?

这是 firebase 教程中的旧代码 https://www.firebase.com/docs/web/libraries/ionic/guide.html

This is the old code from the firebase tutorial 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 引用.我们如何在 ionic 2 和 typescript 中做到这一点

which just includes cdn references to the Firebase library. How would we do this in ionic 2 and typescript

推荐答案

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

There is no bootstrap in ionic2 apps...

  • 您可以为 angularfire2firebase
  • 加载 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/ionic2-angularfire-sample

full source in 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()
        }
    })
}

查看代码

这篇关于如何使用 ionic 2/angular 2 和 typescript 设置 firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆