首页 文章

TypeError:无法读取NativeScript应用程序中未定义的属性'firebase'

提问于
浏览
0

我正在使用带有AngularJS2和TypeScript的NativeScript创建移动应用程序 . 使用Firebase作为DB来存储数据 .

为了在NativeScript中使用firebase,我起诉了以下库 - https://github.com/EddyVerbruggen/nativescript-plugin-firebase

这是我的代码 -

import {Component} from "@angular/core";
import {User} from "./shared/user/user";
import {UserService} from "./shared/user/user.service";
import {HTTP_PROVIDERS} from "@angular/http";
var firebase = require("nativescript-plugin-firebase");

@Component({
  selector: "my-app",
  providers: [UserService, HTTP_PROVIDERS],
  templateUrl: "pages/login/login.html",
  styleUrls: ["pages/login/login-common.css", "pages/login/login.css"]
})

export class AppComponent {
    user: User;
    isLoggingIn = true;

    constructor(private _userService: UserService) {
        this.user = new User();
    }

    submit() {
        if (this.isLoggingIn) {
            this.login();
        } else {
            this.signUp();
        }
    }

    login() {
        // TODO: Define
        console.log('Clicked on Login button');

        firebase.init({
            persist: true // Allow disk persistence. Default false.
        }).then(
            function (instance) {
                console.log("firebase.init done");
            },
            function (error) {
                console.log("firebase.init error: " + error);
            }
        );
    }

    signUp() {
        this._userService.register(this.user);
    }

    toggleDisplay() {
        this.isLoggingIn = !this.isLoggingIn;
    }
}

方法当用户单击“登录”按钮时,将调用“Login()” . 我已经粘贴了firebase代码,用于检查firebase是否正常 .

但是,只要调用“Login()”,它就会显示以下错误 .

JS: Clicked on Login button
JS: Error in firebase.init: TypeError: Cannot read property 'firebase' of undefined
JS: firebase.init error: TypeError: Cannot read property 'firebase' of undefined

我可以理解这个错误正在显示,因为我没有在类中正确插入“firebase”依赖项 . 但是,由于我是TypeScript的新手,我无法理解......如何做到这一点 .

需要一些帮助

编辑 -

使用 import * as firebase from "nativescript-plugin-firebase"; 而不是 var firebase = require("nativescript-plugin-firebase"); 时从VS代码获取以下错误

enter image description here

2 回答

  • 0

    import firebase = require("nativescript-plugin-firebase"); 在reference.d.ts中添加 /// <reference path="./node_modules/nativescript-plugin-firebase/firebase" /> (没有扩展名)

  • 0

    您应该在app.gradle中启用multiDex并删除并重建应用程序

    android {
     defaultConfig {
     applicationId "com.mysite.myproj"
     generatedDensities = []
     multiDexEnabled true
    }
    

    请参阅This issue表格了解更多详情

相关问题