简体   繁体   English

如何使用 firebase 和文档或 window 的导入从 html 文件中获取信息

[英]How to use imports for firebase and document or window to get info from the html file

When I use imports enabling type:module in the package json file I can use without problems the imports such as import { ref, set,child,getDatabase,onValue,get,push, update} from "firebase/database";当我在 package json 文件中使用导入启用类型:模块时,我可以毫无问题地使用导入,例如 import { ref, set,child,getDatabase,onValue,get,push, update} from "firebase/database"; and access the data, but I want to interact with that information with inputs or text areas but I cannot access those it throws document is not defined.!并访问数据,但我想通过输入或文本区域与该信息进行交互,但我无法访问它抛出的那些 document is not defined.!

I have read many pages but as I am new I cannot get it.我已经阅读了很多页,但由于我是新手,所以我无法理解。 I cannot apply the answer to my project.我无法将答案应用到我的项目中。

import { ref, set,child ,getDatabase,onValue,get,push, update} from "firebase/database";
import { getAuth, onAuthStateChanged } from "firebase/auth";
//to get info from firebase

var headbox = document.getElementById('a'); // from Html to manage info send and show it here
var bodybox = document.getElementById('b'); // error when I want to read this

Although if I take those imports out and erase type:module in the package json file, I can use document.getById but I cannot use imports please help me!尽管如果我取出这些导入并擦除 package json 文件中的类型:模块,我可以使用 document.getById 但我不能使用导入请帮助我! I am new我是新的

type: module in package.json is for use in Nodejs environments, hence the error. type: module package.json Nodejs 环境,因此出现错误。 Backend has no document or even window object.后端没有document甚至window object。

What you are doing is frontend, so setting up a frontend environment using vite / webpack (preferably vite ), would do you some justice.你正在做的是前端,所以使用vite / webpack (最好vite )设置一个前端环境,对你有好处。

vite is really easy to setup for what you are doing. vite真的很容易设置你正在做的事情。 Vite Getting Started Guide . Vite 入门指南

You just place your index.html at the root of your working folder and run it's commands.您只需将index.html放在工作文件夹的根目录下并运行它的命令。 It loads very quickly.它加载非常快。

The Firebase Web SDK ideally requires module bundlers (Webpack/Rollup) or JavaScript framework tooling like Vue, React, Angular, etc because the Web SDK (or at least version 9) is optimized to work with module bundlers to eliminate unused code (tree-shaking) and decrease SDK size. The Firebase Web SDK ideally requires module bundlers (Webpack/Rollup) or JavaScript framework tooling like Vue, React, Angular, etc because the Web SDK (or at least version 9) is optimized to work with module bundlers to eliminate unused code (tree-晃动)并减小 SDK 大小。

Sounds like you would be better off using CDN (content delivery.network).听起来您最好使用 CDN(内容交付网络)。

JavaScript JavaScript

import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js';
import { ref, set, child, getDatabase, onValue, get, push, update } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js';
import { getAuth, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js';
        
const firebaseConfig = {
    apiKey: 'xxx',
    authDomain: 'xxx',
    projectId: 'xxx',
    storageBucket: 'xxx',
    messagingSenderId: 'xxx',
    appId: 'xxx',
    measurementId: 'xxx'
};
const firebaseApp = initializeApp(firebaseConfig);
const auth = getAuth();

window.addEventListener('DOMContentLoaded', (event) => {
    const headbox = document.getElementById('a');
    const bodybox = document.getElementById('b');
    
    onAuthStateChanged(auth, (user) => {
        if (user) {
            // user.uid
        }
    });
});

HTML HTML

<html>
<head>
  ...
</head>
<body>
  <div id="a">Head Box</div>
  <div id="b">Body Box</div>
  <script type="module" src="your-javascript-file.js"></script>
</body>
</html>

暂无
暂无

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

相关问题 如何从 firebase 获取单个文档信息? - How to get a single doc info from firebase? 如何在上传文件到Firebase存储之前获取自动生成的文档ID,并将该ID用作Web的存储参考? - How to get auto-generated document ID before uploading file to Firebase storage, and use that ID as a storage reference for Web? 从 firebase 文档中获取集合名称 - Get collection name from firebase document 如何获取firebase firestore中完整的文档路径? - How to get the complete document path in firebase firestore? 如何从 Firebase 中获取值并使用 JavaScript 添加到 HTML 表中 - How to get Value from Firebase and add in HTML Table using JavaScript Firebase FireStore:如何在使用 addDoc() 添加文档后获取文档快照 - Firebase FireStore : How to get document snapshot after adding document with addDoc() 如何在firebase存储中上传一张图片,在firebase文档字段中获取url - How to upload a picture in firebase storage and get it url in firebase document field Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents 获取firebase文档的ID - Get the ID of firebase document 如何获取从 Flutter 中的 Firebase Firestore 检索到的文档列表中存在的特定文档的索引? - How to get the index of a specific document present in the list of documents retrieved from Firebase Firestore in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM