我正试图将firebase添加到typescript应用程序中,但我在初始配置中遇到问题。我已经设置了等式的谷歌一边,并在配置中使用它。尽管如此,我还是有问题:
import firebase from "firebase/app";
import "firebase/database";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
//fields below purposefully redacted
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase.database(firebaseConfig["appId"]);
错误:
Property 'database' does not exist on type 'typeof import("c:////Documents/GitHub//node_modules/firebase/app/dist/app/index")'.ts(2339)
以下是问题的图片:
代码片段错误消息
如果你正在使用V9.0.0+
,那么你应该升级到新的模块化语法,看起来像这样:
import { initializeApp } from "firebase/app"
import { getDatabase } from "firebase/database"
const app = initializeApp({...config})
const db = getDatabase(app)
export { db }
确保在代码的其他地方使用新语法。参考文档了解更多关于新语法的信息。
如果您希望使用旧的名称间隔语法(如在大多数旧教程中),请使用compat
版本:
import firebase from "firebase/compat/app";
import "firebase/compat/database";
// import "firebase/compat/[SERVICE_NAME]"