[英]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.要成为某事并需要验证,还有很长的路要走,但它回答了您的问题。
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.