来自 CDN 的 Firebase 9.6.6 JS 出错

Firebase 9.6.6 JS from CDN errors out

我正在尝试为我的一个项目挂钩基于 CDN 的 firebase 库,并且根据文档它是可行的 https://firebase.google.com/docs/web/alt-setup

但是当我在下面输入 html/js 代码时

<html>
<body>
  <script type="module">
    import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js'
    // Add Firebase products that you want to use
    import { auth } from 'https://www.gstatic.com/firebasejs/9.6.6/firebase-auth.js'
    const firebaseConfig = {
        apiKey: "<>",
        authDomain: "<>.firebaseapp.com",
        projectId: "<>",
        storageBucket: "<>.appspot.com",
        messagingSenderId: "<>",
        appId: "<>",
        measurementId: "<>"
   };
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
  </script>
</body>
</html>

它的错误输出如下(在控制台上看到)

Uncaught SyntaxError: import not found: auth

我不想回退到旧版本的 firebase,所以有人可以建议任何解决方案吗?

您正在导入模块化 SDK,它不会导出 auth 符号。要访问 auth 服务,请导入 getAuth 函数:

import { getAuth } from 'https://www.gstatic.com/firebasejs/9.6.6/firebase-auth.js'

然后将其命名为:

const auth = getAuth();

// And then for example: createUserWithEmailAndPassword(auth, email, password)

我建议在开始时随身携带 getting started with authentication on the web 上的 Firebase 文档,因为它有方便的 copy/pasteable 代码片段用于此类常见操作。