3

我正在按照官方 Google 教程示例通过 CDN 而不是 SDK 包含 Firebase:

https://firebase.google.com/docs/web/alt-setup#from-the-cdn

逐字复制示例后:

<body>
  <!-- Insert this script at the bottom of the HTML, but before you use any Firebase services -->

  <script type="module">
    import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.4.1/firebase-app.js'

    // If you enabled Analytics in your project, add the Firebase SDK for Google Analytics
    import { analytics } from 'https://www.gstatic.com/firebasejs/9.4.1/firebase-analytics.js'

    // Add Firebase products that you want to use
    import { auth } from 'https://www.gstatic.com/firebasejs/9.4.1/firebase-auth.js'
    import { firestore } from 'https://www.gstatic.com/firebasejs/9.4.1/firebase-firestore.js'
  </script>
</body>

我得到浏览器错误:

未捕获的语法错误:请求的模块“https://www.gstatic.com/firebasejs/9.4.1/firebase-auth.js”未提供名为“auth”的导出

将 auth import 行注释掉,你会得到与 firestore 导入行完全相同的错误。

这是文件/教程的问题还是我的问题?

似乎教程指的是错误的文件?

4

1 回答 1

2

看起来添加的代码示例是错误的。请尝试使用下面的代码来使用 9+ 版 SDK for web

访问https://firebase.google.com/docs/auth/web/password-auth#sign_in_a_user_with_an_email_address_and_password

    从“https://www.gstatic.com/firebasejs/9.5.0/firebase-auth.js”导入 { getAuth, signInWithEmailAndPassword }
    
    const auth = getAuth();
    signInWithEmailAndPassword(身份验证,电子邮件,密码)
      .then((userCredential) => {
        // 登录
        常量用户 = userCredential.user;
        // ...
      })
      .catch((error) => {`在此输入代码`
        常量错误代码 = 错误代码;
        常量 errorMessage = error.message;
      });

于 2021-12-01T06:41:13.457 回答