无法将 React 网站与 Firestore 连接
Cannot connect React website with Firestore
我尝试将我的网站与 Firestore 连接,但没有成功。
这是我的 config.js
import firebase from 'firebase/app'
const firebaseConfig = {
//cred
};
firebase.initializeApp(firebaseConfig);
export default firebase;
这是Signup.js
import React, { Component } from "react";
import firebase from "./config";
class Signup extends Component {
constructor(props) {
super(props);
this.state = {
name: '-',
email: '-',
};
}
render(){
return (
//do something
)}
}
总是报错:
./src/config.js
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
当我改变config.js
import firebase from 'firebase/app'
至
import firebase from 'firebase'
它给出了这个错误:
./src/config.js
Module not found: Can't resolve 'firebase' in 'C:\Users\NITRO\register\src'
如果我再次更改 Config.js
import * as firebase from 'firebase/app'
这是一个错误:
TypeError: Cannot read properties of undefined (reading 'initializeApp')
我不知道它有什么问题。
请帮我。
谢谢。
您应该用这样的命名导出替换默认导出:
import { initializeApp } from 'firebase/app';
// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
//...
};
const app = initializeApp(firebaseConfig);
有关详细信息,请查看此处:https://firebase.google.com
我尝试将我的网站与 Firestore 连接,但没有成功。
这是我的 config.js
import firebase from 'firebase/app'
const firebaseConfig = {
//cred
};
firebase.initializeApp(firebaseConfig);
export default firebase;
这是Signup.js
import React, { Component } from "react";
import firebase from "./config";
class Signup extends Component {
constructor(props) {
super(props);
this.state = {
name: '-',
email: '-',
};
}
render(){
return (
//do something
)}
}
总是报错:
./src/config.js
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
当我改变config.js
import firebase from 'firebase/app'
至
import firebase from 'firebase'
它给出了这个错误:
./src/config.js
Module not found: Can't resolve 'firebase' in 'C:\Users\NITRO\register\src'
如果我再次更改 Config.js
import * as firebase from 'firebase/app'
这是一个错误:
TypeError: Cannot read properties of undefined (reading 'initializeApp')
我不知道它有什么问题。 请帮我。 谢谢。
您应该用这样的命名导出替换默认导出:
import { initializeApp } from 'firebase/app';
// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
//...
};
const app = initializeApp(firebaseConfig);
有关详细信息,请查看此处:https://firebase.google.com