简体   繁体   English

如何导入 firebase 应用程序作为浏览器模块导入?

[英]How can I import firebase app as browser module import?

I am trying to import firebase app (8.0.2) on the client side without bundler.我正在尝试在没有捆绑程序的客户端上导入 firebase 应用程序(8.0.2)。 I downladed the firebase-app.js from the CDN and host it on my server.我从 CDN 下载了 firebase-app.js 并将其托管在我的服务器上。

The goal is to have the firebase app module imported in one of my modules like this:目标是在我的一个模块中导入 firebase 应用程序模块,如下所示:

import firebase from '/vendor/firebase-app.js';

However, when trying to access firebase, I am getting an error about the default export not being defined.但是,当尝试访问 firebase 时,我收到有关未定义默认导出的错误。 In firefox, this is:在 firefox 中,这是:

Uncaught SyntaxError: import not found: default未捕获的语法错误:找不到导入:默认

Where am I wrong?我哪里错了? Does the module import only work with a bundler?模块导入是否仅适用于捆绑器? I believe I have some fundamental misunderstanding, but I cant figure it out.我相信我有一些基本的误解,但我无法弄清楚。

Thanks for the help!谢谢您的帮助!

As Doug comments, that syntax is intended for use with bundlers.正如 Doug 评论的那样,该语法旨在与捆绑器一起使用。

If you want to import the SDK inside a module script then you will need to use a CDN, like this:如果要在模块脚本中导入 SDK,则需要使用 CDN,如下所示:

useEffect(() => {
    let isSubscribed = true
    const initFirebase = async () => {
        try {
            const [{
                    initializeApp
                },
                {
                    getFirestore,
                    collection,
                    addDoc
                }
            ] = await Promise
                .all([
                    import('https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js'),
                    import('https://www.gstatic.com/firebasejs/9.6.10/firebase-firestore.js')
                ])

            const fireApp = initializeApp(firebaseConfig)
            const appDB = getFirestore(fireApp)

            if (isSubscribed) {
                setFirebase(fireApp)
                setDB(appDB)
                setResult('success')
            }
        } catch (e) {
            console.log(e);
            setResult('error')
        }
    }

    if (firebaseConfig) {
        initFirebase()
            .catch(e => {
                console.log(e);
            })
    }

    return () => is Subscribed = false
}, [firebaseConfig])

That's from a POC I'm working on, single HTML file, Preact + Firebase, no bundlers needed.那来自我正在研究的 POC,单个 HTML 文件,Preact + Firebase,不需要捆绑器。

It is still a long way to be something and need validations, but it answers your question.要成为某事并需要验证,还有很长的路要走,但它回答了您的问题。

Here is the repo .这是回购

In Firebase "^9.9.4" use:在 Firebase "^9.9.4" 中使用:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {}
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app)

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

相关问题 Flutter web app & firebase:不能在模块外使用导入语句 - Flutter web app & firebase : Cannot use import statement outside a module 如何解决此 firebase 导入错误? 找不到模块:错误:Package 路径。 不从 package 导出 - How do I resolve this firebase import error? Module not found: Error: Package path . is not exported from package 我无法导入 firebase - I cant import firebase 如何为 firebase 云函数导入基于模块的依赖项? - How to import module based dependencies for firebase cloud functions? 当我使用 import * as firebase from 'firebase' 导入 firebase 时;? - when I import firebase using import * as firebase from 'firebase';? 如何从Angular App中的firebase cdn文件导入AngularFireStorage - How to import AngularFireStorage from firebase cdn files in Angular App 我可以使用 jQuery 将数据导入 Firebase Firestore 数据库吗? - Can I import data to Firebase Firestore database using jQuery? 如何将 BigQuery 中的细分导入 Firebase? - How do I import segments from BigQuery into Firebase? 如何在 AWS Lambda 上导入正则表达式 - How can I import regex on AWS Lambda 未找到模块:错误:您尝试导入项目 src/ 目录之外的 /firebase/app - Module not found: Error: You attempted to import /firebase/app which falls outside of the project src/ directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM