[英]Uncaught TypeError: Cannot read property 'initializeApp' of undefined
I'm trying to use a few Firebase libraries in my Vue project (I installed it using npm install firebase
).我正在尝试在我的 Vue 项目中使用一些 Firebase 库(我使用
npm install firebase
安装它)。
I added those in main.js
:我在
main.js
中添加了这些:
import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'
Firebase.initializeApp({
apiKey: 'xxx',
authDomain: 'xxx',
databaseURL: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
measurementId: 'xxx'
})
And I get:我得到:
Uncaught TypeError: Cannot read property 'initializeApp' of undefined
未捕获的类型错误:无法读取未定义的属性“initializeApp”
Change this:改变这个:
import { Firebase } from 'firebase/app'
into this:进入这个:
import * as firebase from "firebase/app";
import everything from firebase/app
then do:从
firebase/app
导入所有内容,然后执行以下操作:
firebase.initializeApp({
apiKey: 'xxx',
authDomain: 'xxx',
databaseURL: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
measurementId: 'xxx'
})
Check your Firebase version in your package.json
file.在
package.json
文件中检查 Firebase 版本。 If it is 9+ it should be something like this:如果它是 9+,它应该是这样的:
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
import { getAuth } from "firebase/auth";
and then:接着:
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "...",
};
// Initialize Firebase
const firebase = initializeApp(firebaseConfig);
const db = getFirestore(firebase);
const auth = getAuth(firebase);
Change this:改变这个:
import { Firebase } from 'firebase/app'
into this:进入这个:
import * as firebase from "firebase/app";
import everything from firebase/app then do:从 firebase/app 导入所有内容,然后执行:
firebase.initializeApp({
apiKey: 'xxx',
authDomain: 'xxx',
databaseURL: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
measurementId: 'xxx'
})
For newcomers using the newest Firebase Modular API (v9) a refactoring is required.对于使用最新Firebase Modular API (v9)的新手,需要进行重构。
The imports must be changed to this:导入必须更改为:
import { FirebaseApp, initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getDatabase } from "firebase/database";
const firebaseConfig = {...}
// Initialize Firebase
const app: FirebaseApp = initializeApp(firebaseConfig);
const db = getDatabase(app);
const auth = getAuth(app);
About this new refactoring style here关于这种新的重构风格在这里
And more information on how Read and Write data changed here有关读取和写入数据如何更改的更多信息,请点击此处
Error that appears caused by the difference of the version you use with the installed SDK version您使用的版本与安装的SDK版本不同导致出现的错误
Simply change from like this:只需像这样更改:
import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'
to this:对此:
import { Firebase } from 'firebase/compat/app'
import 'firebase/compat/analytics'
import 'firebase/compat/auth'
import 'firebase/compat/messaging'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.