【问题标题】:An error occurred while creating an application on firebase@9.0.0-beta.2 + NUXT在 firebase@9.0.0-beta.2 + NUXT 创建应用程序时出错
【发布时间】:2021-09-06 23:02:49
【问题描述】:

我使用 firebase@9.0.0-beta.2 创建了一个应用程序 我得到了错误

创建文件夹plugins 添加文件firebase.js

import { initializeApp } from 'firebase/app'
import { getFirestore } from 'firebase/firestore'
const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  databaseURL: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: ''
}
let firebaseApp
try {
  firebaseApp = getApp()
} catch (e) {
  firebaseApp = initializeApp(firebaseConfig)
}
const db = getFirestore(firebaseApp, {})
export { db }

nuxt.config.js

...
plugins: [    
    '~/plugins/firebase.js'
  ],
...

我得到错误:

error 'getApp' is not defined no-undef

【问题讨论】:

  • 这个在本地工作吗?该错误表明您没有getApp,并且确实在您共享的sn-p中的任何地方都看不到它。

标签: firebase vue.js nuxt.js


【解决方案1】:

getApp() 不是内置方法,必须从相应的库中调用,在本例中为:FirebaseApp.getApp()

import { initializeApp, getApps, getApp } from "firebase/app";
getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();

或

if (FirebaseApp.getApps(context).isEmpty()) {
    FirebaseApp.initializeApp(context);
}

【讨论】:

  • 此选项有效import { initializeApp, getApp } from 'firebase/app'
猜你喜欢
  • 2020-09-15
  • 2019-10-22
  • 2020-02-07
  • 2023-04-07
  • 2021-01-10
  • 2017-03-08
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
相关资源
最近更新 更多