Firebase导入未在构造函数离子本机中初始化

时间:2019-02-19 11:22:35

标签: firebase ionic-framework npm ionic3 ionic-native

我正在处理 ionic fcm推送通知。我已经导入

import { Firebase } from '@ionic-native/firebase';,但无法在构造函数中初始化Firebase。这是我的代码

import { Injectable } from '@angular/core';
import { Firebase } from '@ionic-native/firebase';
import { Platform } from 'ionic-angular';
import {HttpClient, HttpHeaders} from '@angular/common/http';

@Injectable()
export class FcmProvider {

  constructor(private platform: Platform, 
              public firebaseNative:Firebase,
              public http: HttpClient) {
    console.log('Hello FcmProvider Provider');
  }

  // Get permission from the user
  async getToken() {
    let token;

    if (this.platform.is('android')) {
      token = await this.firebaseNative.getToken()
    }      
  }
  }
}

我在

遇到错误
constructor(private platform: Platform, 
              public firebaseNative:Firebase,

是说

  

找不到名称Firebase

我正在关注this article

我已经安装了 nionic @ ionic-native / firebase npm i @ ionic-native / fcm 软件包。有关更多信息,请参见屏幕截图:

enter image description here

更新:请 Package.Json

  "private": true,
  "dependencies": {
    "@angular/common": "^7.2.2",
    "@angular/core": "^7.2.2",
    "@angular/forms": "^7.2.2",
    "@angular/http": "^7.2.2",
    "@angular/platform-browser": "^7.2.2",
    "@angular/platform-browser-dynamic": "^7.2.2",
    "@angular/router": "^7.2.2",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/fcm": "^5.1.0",
    "@ionic-native/firebase": "^5.1.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^4.0.0",
    "angularfire2": "^5.1.1",
    "cordova-plugin-firebase": "2.0.5",
    "core-js": "^2.5.4",
    "firebase": "^5.8.3",
    "ionic-angular": "^3.1.0",
    "nvm-win": "^0.2.4",
    "reinstall": "^2.0.0",
    "rxjs": "~6.3.3",
    "zone.js": "~0.8.29"
  },
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

可能您正在使用ionic版本3项目,并且正在使用  离子V4的最新版本。

  

第一个解决方案

如v4文档中所述,在导入路径的最后使用“ ngx”

赞:-

从'@ ionic-native / firebase / ngx'导入{Firebase}; 参考:https://ionicframework.com/docs/native/firebase

  

第二个解决方案

首先删除现有插件 离子cordova插件删除cordova-plugin-firebase

使用

重新添加

ionic cordova插件添加cordova-plugin-firebase

npm install --save @ ionic-native / firebase @ 4

请记住要遵循doc v3进行进一步的实现

https://ionicframework.com/docs/v3/native/firebase/

答案 1 :(得分:2)

由于您使用的是比Angular 6大的Angular版本,因此需要在导入末尾包含/ngx。即import { Firebase } from '@ionic-native/firebase/ngx';

答案 2 :(得分:2)

如果您为项目类型安装了错误的本机插件版本,或者在导入结束时未附加 ngx ,则会出现错误。

ionic.config.json

中检查项目类型

如果类型为“ 离子角”(通常为Ionic 3),则安装4.x.x版本。

npm i -s @ionic-native/firebase@4

如果类型为“角度” (通常为Ionic 4),则安装最新版本

npm i -s @ionic-native/firebase

注意:

仅当您使用Angular 6时,才在导入末尾添加ngx

import { Firebase } from '@ionic-native/firebase/ngx';

如果不从导入中删除ngx

import { Firebase } from '@ionic-native/firebase'

参考https://github.com/ionic-team/ionic/issues/15225#issuecomment-414074074