将ionic angularfire2项目纳入另一个离子项目

时间:2019-06-13 03:44:44

标签: firebase ionic-framework angularfire2

我们有一个使用angularfire2与Firebase Realtime Database连接的应用程序。 app.module.ts如下所示


import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from '@angular/fire/database';

export const firebaseConfig = {
  apiKey: "XXXX-XXX-XX",
    authDomain: "xxxx.firebaseapp.com",
    databaseURL: "https://xxx.firebaseio.com",
    projectId: "xxx",
    storageBucket: "xxx.appspot.com",
    messagingSenderId: "xxxx",
    appId: "1:xxxxxx:web:xxxxxx"
};

@NgModule({
  declarations: [
    AppComponent
  ],
  entryComponents: [],
  imports: [
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireDatabaseModule,
    BrowserModule, 
    IonicModule.forRoot(), 
    AppRoutingModule,
  ],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

像下面这样的一项服务

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';
import { MitAnalyticsObject } from './model/mit-analytics-object.model';

@Injectable({
  providedIn: 'root'
})
export class MitBotAnalyticsService {

  constructor(public db:AngularFireDatabase) { }

  log(orgName:string, sessionId:string, intent:string, lastUtterance:string, msg:string, time:number, sender:string, os:string, device:string, uid:string){

    var anaObj = new MitAnalyticsObject(msg, time, sender, os, device, uid, sessionId, intent, lastUtterance)

    console.log("Logging obj:" + JSON.stringify(anaObj))
    return new Promise((resolve, reject) => {
      this.db.list(orgName + "/messages/").push(anaObj).then
      (
        (res:any) => {
          resolve(res)
        }
      ),
      err => { 
        reject(err)
      }
  })
  }
}

和下面的简单对象定义:

export class MitAnalyticsObject {
    private _msg:string
    private _time:number
    private _sender:string
    private _os:string
    private _device:string
    private _uid:string //userid or username
    private _sessionId:string
    private _intent:string
    private _lastUtterance:string

    constructor(msg:string, time:number, sender:string, os:string, device:string, uid:string, sessionId:string, intent:string, lastUtterance:string){
        this._msg = msg
        this._time = time
        this._sender = sender
        this._os = os
        this._device = device
        this._uid = uid
        this._sessionId = sessionId
        this._intent = intent
        this._lastUtterance  = lastUtterance
    }
}

没有UI组件或任何东西。它被添加到一个空白的离子项目中,因此它具有我实际上不需要的空白页面组件。

要求是能够将上述项目转换为可重用的片段,我可以简单地导入该片段或像库一样包含和使用它。

主机项目只能是离子应用程序。

我从没做过,所以想知道如何实现?

0 个答案:

没有答案