javascript - Firebase 9 模块化 - 如何开始
问题描述
我尝试开始使用 Firebase 9。我使用 VSCode 并使用 npm 导出(在 vscode 中)
<script src="https://www.gstatic.com/firebasejs/9.0.0-beta.5/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.0-beta.5/firebase-analytics-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.0-beta.5/firebase-auth-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.0-beta.5/firebase-firestore-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.0-beta.5/firebase-storage-compat.js"></script>
<!-- init -->
<script defer src="firebase/init.js" type="text/javascript"></script>
<script type="module">
import {
getAuth,
onAuthStateChanged
} from "firebase/auth";
const auth = getAuth(firebaseApp);
onAuthStateChanged(auth, user => {
console.log(user)
});
</script>
错误
未捕获的类型错误:无法解析模块说明符“firebase/auth”。相对引用必须以“/”、“./”或“../”开头。
解决方案
我遇到了同样的问题,并且能够通过在 5:40 时间戳附近仔细观看 David 的视频来解决它。
这是在本地服务时工作的基本样板:
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js';
import { getAuth, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-auth.js';
const firebaseApp = initializeApp({
// (insert your Firebase configuration here)
});
const auth = getAuth(firebaseApp);
onAuthStateChanged(auth, user => {
if (user) {
console.log('Logged in as ${user.email}' );
} else {
console.log('No user');
}
});
<!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">
<title>Getting started with Firebase Auth</title>
<script type="module" src="/index.js"></script>
</head>
<body>
</body>
</html>
推荐阅读
- wordpress - WooCommerce CSV 导出 - Excel 中的字符显示不正确
- laravel - 如何使用电子邮件和密码通过 Laravel 基本身份验证登录 Flutter 应用程序
- c# - 尝试使用 Tpm2Lib 创建主键时出现 BadAuth 响应
- android - 如何使用 DateTimeFormatter 转换为字符串
- javascript - 如何将焦点设置在 Vuejs 2 中 v-for 内呈现的 textarea 上?
- gerrit - 如何限制对自定义 gerrit 插件的访问
- apache-spark - 如何修复“BlockManagerMasterEndpoint - 没有更多副本可用于 rdd”问题?
- clips - 对一组事实 CLIPS 进行排序
- java - 为什么将信息从 Java 代码更新到 SQL 数据库时会出现错误?
- excel-formula - 索引 Minif excel