未定义反应 firebase initializeApp

react firebase initializeApp is not defined

我正在尝试在 React 应用程序上使用 firebase 存储, 但我得到一个错误,我不知道来自哪里

第 15:15 行:'initializeApp' 未定义

这是我的config.js文件

感谢帮助

import firebase from "firebase/compat/app";
import 'firebase/storage';
import 'firebase/firestore';

const firebaseConfig = {
    apiKey: "//myapikey",
    authDomain: "//firebase name.firebaseapp.com",
    projectId: "//firebase name",
    storageBucket: "//firebase name.appspot.com",
    messagingSenderId: "156082439629",
    appId: "//appid..."
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

const projectStorage = firebase.storage();
const projectFirestore = firebase.firestore();

export {
    projectStorage,
    projectFirestore
};

如果您想使用 compat 版本的 Firebase SDK,那么您还必须导入其他 Firebase 服务的兼容版本。尝试重构代码,如下所示:

import firebase from "firebase/compat/app";
import 'firebase/compat/storage';
import 'firebase/compat/firestore';

const firebaseConfig = {...};

// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
// with firebase. namespace

const projectStorage = firebase.storage();
const projectFirestore = firebase.firestore();

export {
    projectStorage,
    projectFirestore
};

但是,我建议使用 Modular SDK 并像这样初始化 Firebase:

import { initializeApp } from "firebase/app";
import { getStorage } from 'firebase/storage';
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {...};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

const projectStorage = getStorage();
const projectFirestore = getFirestore();

export {
    projectStorage,
    projectFirestore
};

也结帐Getting started with Firebase for the web