当我添加firebase sdk i时,javascript函数不起作用



我想在外部JavaScript文件中添加到firebase SDK,并将我的JavaScript文件与index.html链接,我想在一个外部JavaScript文件创建所有函数,但在放入SDK代码后,我的函数无法工作,控制台中也没有输出。

//main.js 代码

import { initializeApp } from "firebase/app";

// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBbXn11EFZ-B4UutkGbbvFFhddnqWFpDWc",
authDomain: "test-app-c0aed.firebaseapp.com",
projectId: "test-app-c0aed",
storageBucket: "test-app-c0aed.appspot.com",
messagingSenderId: "797893271922",
appId: "1:797893271922:web:5fc8bade62add0ca573deb"
};

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

const provider = new firebase.auth.GoogleAuthProvider();

// event and function for login button
document.getElementById('login').addEventListener("click",login);
function login()
{
console.log('login');
firebase.auth().signInWithPopup(provider).then( res => {
console.log(res);
})

}
// event and function for log out button
document.getElementById('logout').addEventListener("click",logout);
function logout(){
console.log('logout');
}

//index.html 代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="container">
<div id="dashboard">
<div id="userDetails">
<p>Welcome to dashboard</p>
</div>
<div id="logout">
<button id="logoutButton">Logout</button>
</div>
</div>
<div id="loginForm">
<button id="login">SignInWithGoogle</button>
</div>
</div>
<script src="main.js"></script>
</body>
</html>

尝试添加type="模块";编写<script type="module" src="main.js"></script>脚本并使用本地服务器托管index.html,因为当您在浏览器file://中打开index.html时,模块将不起作用

最新更新