【问题标题】:how to set up firebase with ionic 2 / angular 2 and typescript如何使用 ionic 2 / angular 2 和 typescript 设置 firebase
【发布时间】:2016-07-31 13:21:49
【问题描述】:

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

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

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

  3. 我应该使用类型来安装 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>

app.js

angular.module("starter", ["ionic", "firebase"])

其中仅包含对 Firebase 库的 cdn 引用。我们如何在 ionic 2 和 typescript 中做到这一点

【问题讨论】:

    标签: typescript angular ionic2


    【解决方案1】:

    您需要在 SystemJS 配置中同时配置 Firebase 和 Angularfire2:

    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) {
    
      }
    }
    

    【讨论】:

    • systemjs是离子配置文件还是angular 2文件?
    • 这是您需要在主 HTML 文件中定义的一些代码。
    【解决方案2】:

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

    • 您可以为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

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

    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()
            }
        })
    }
    

    See Code Here

    【讨论】:

    • 哦,酷,这看起来像我要找的东西,还有一个问题 - rxjs 是否包含在 ionic 中,还是我需要以某种方式从外部引用的东西 - 使用 npm 安装是否会自动使其可用于从 ionic 代码导入 - 还是通过 npm 添加的每个新库都需要提供程序?
    • 查看 github repo 中的 package.json,它将列出所有需要的模块
    • 如何使用 angularfire2 订阅登录/注销事件?
    • 编辑了答案...可能应该是一个新问题。
    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2020-02-25
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多