繁体   English   中英

Firebase 9 模块化 - 如何开始

[英]Firebase 9 modular -how to start

我尝试开始玩 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 timestamp 附近仔细观看 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>

这就是我现在正在使用的(谢谢彼得)

 // initialize 
import {
    initializeApp
} from 'https://www.gstatic.com/firebasejs/9.6.8/firebase-app.js';

const firebaseConfig = {
    apiKey: "xxx",
    authDomain: "xxx",
    projectId: "xxx",
    storageBucket: "xxx.appspot.com",
    messagingSenderId: "xx",
    appId: "1:xx:web:xx",
    measurementId: "G-xx"
};
const firebaseApp = initializeApp(firebaseConfig);

// Add Firebase products that you want to use
import {
    getAuth,
    onAuthStateChanged,
    signInWithEmailAndPassword,
    signOut
} from 'https://www.gstatic.com/firebasejs/9.6.8/firebase-auth.js';

import {
    getFirestore,
    collection,
    getDocs,
    getDoc,
    doc,
    setDoc
} from 'https://www.gstatic.com/firebasejs/9.6.8/firebase-firestore.js';
import {
    getStorage,
    ref,
    uploadBytesResumable,
    getDownloadURL,
    listAll,
    deleteObject
} from "https://www.gstatic.com/firebasejs/9.6.8/firebase-storage.js";
//============================================
//============================================
const db = getFirestore();
const auth = getAuth();
let UID;
window.addEventListener('DOMContentLoaded', (event) => {
    onAuthStateChanged(auth, (user) => {
        if (user) {
            UID = user.uid;
            //Your code...
        }
    });

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM