- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在通过我的 html 表单将数据存储到我的数据库时遇到问题,我得到的错误来 self 的导入语句,它是:未捕获的语法错误:无法在模块外部使用导入语句!
代码如下:
import {
initializeApp
} from "firebase/app";
const firebaseConfig = {
apiKey: "MY_API_KEY", //actual values are in my code, changed here for security purposes.
authDomain: "My_auth_Domain",
databaseURL: "FirebaseDb_URL",
projectId: "Project_id",
storageBucket: "storageBucket",
messagingSenderId: "SenderId",
appId: "appId"
};
const app = initializeApp(firebaseConfig);
let inviteInfo = firebase.database().ref("inviteinfos");
document.getElementById("#regform", submitForm);
function submitForm(e) {
let name = document.getElementsByName("fullname").value;
let compName = document.getElementsByName("compName").value;
let email = document.getElementsByName("email").value;
let address = document.getElementsByName("address").value;
let contact = document.getElementsByName("contact").value;
console.log(name, compName, email, address, contact);
saveContactInfo(name, compName, email, address, contact);
document.getElementById("#regform").reset();
}
到目前为止我尝试了什么:
const {initializeApp} = require("firebase/app");
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
const {initializeApp} = require("https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js");
npm 安装 firebase
来检索模块。到目前为止,这些都对我不起作用,我尝试将信息存储到的表目前不存在,但研究表明即使在没有表存在的情况下运行也会自动为我创建表。
最佳答案
像这样加载 app.js
:
<script type="module" src="./app.js"></script>
在 app.js
中,您可以像这样导入 firebase-app
:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
// ...
这是一个示例(我不能为 app.js
使用单独的文件,但这表明它确实有效):
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
// Check that it worked and returned a function:
console.log(typeof initializeApp); // "function"
</script>
你根本不需要 firebase-app.js
的 script
标签,它会因为你的 import< 而被浏览器加载
.
请注意,这将在浏览器中本地使用模块。所有现代浏览器都直接支持这样做,但像 Internet Explorer 这样的过时浏览器不支持,并且像这样单独加载模块可能涉及许多小的 HTTP 调用而不是一些较大的调用,这可能导致页面加载延迟,即使使用现代网络协议(protocol)也是如此SPY。出于这个原因,人们经常使用像 Vite、Rollup、Webpack 等打包工具,将模块组合成少量的打包文件。如果你愿意,你可以考虑这样做。但同样,现代浏览器本身就支持模块,因此您不必这样做。
在每个要从 firebase-app 导入的地方使用 "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js"
可能有点痛。您有两种选择可以使它更容易:
1.在基于 Chromium 的浏览器 ( only, sadly, for now ) 中,您可以使用 import map :
<script type="importmap">
{
"imports": {
"firebase-app": "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js"
}
}
</script>
这会告诉您的浏览器,当您从 "firebase/app"
导入时,它应该使用 "https://www.gstatic.com/firebasejs/9.6.6/firebase-应用程序.js”
。
实例:
<script type="importmap">
{
"imports": {
"firebase/app": "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js"
}
}
</script>
<script type="module">
import { initializeApp } from "firebase/app";
// Check that it worked and returned a function:
console.log(typeof initializeApp); // "function"
</script>
2。如果您不能使用导入映射,您可以编写一个模块,从 firebase-app
重新导出所有内容,然后在您的代码中从该模块导入,而不是直接从 firebase-app
:
您自己的本地 firebase-app.js
文件:
// This re-exports all of its named exports (but not its default, if any)
export * from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
/* firebase-app.js doesn't provide a default export. If it did, this is
how you'd re-export it:
export { default } from "https://www.gstatic.com/firebasejs/9.6.6/firebase-app.js";
*/
然后 app.js
将使用 firebase-app.js
:
import { initializeApp } from "./firebase-app.js";
(我不能用 Stack Snippets 做一个活生生的例子,但是 here's that on CodeSandbox。)
关于javascript - 未捕获的语法错误 : Cannot use import statement outside a module (Using Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71139008/
我刚刚通过更改 import * as CodeMirror 修复了一个错误简单明了import CodeMirror . 我复制了this code . (从 TypeScript 移植) impo
我调试(在 PyCharm 中)一个脚本。我在断点处停止,然后转到调试控制台窗口,然后从那里调用导入行,如下所示: import my_util1 from my_utils 然后我调用 my_uti
谁能给我解释一下 import 语句是如何工作的? 例如,我在 myapp/app/models 包中有一个类型 User: package models type User struct {
我想导入 Control.App进入一个引用 PrimIO.PrimIO 的模块通过不合格的名称 PrimIO在很多地方。当然,问题在于 Control.App还导出一个名为 PrimIO 的定义.我
我应该使用 from foo import bar 或者 import foo.bar as bar 当导入模块 还有无需/希望更改名称 (bar)? 有什么不同吗?有关系吗? 最佳答案 假设 bar
我正在 Windows 上使用 Theano 进行深度学习实验的第一步,我很惊讶仅仅加载库需要多少时间。 这是小测试程序: from time import time t0 = time() impo
在 TypeScript 中,如何在不创建任何别名的情况下从文件“导入 *”? 例如我有一个包含顶级导出函数的文件“utils”,我想导入所有这些函数而不为每个函数重新创建别名。 像这样: impor
我应该使用 from foo import bar 或 import foo.bar as bar 当导入模块并且不需要/希望更改名称(bar)? 有什么不同吗?有关系吗? 最佳答案 假设bar是fo
这个问题在这里已经有了答案: Use 'import module' or 'from module import'? (23 个回答) 关闭8年前。 我想知道代码片段之间是否有任何区别 from u
我试过了 from urllib import request mine = request.Request() 和 import urllib.request mine = urllib.reque
所以,我有一个关于 Python 导入的小谜团。我确信出于某种原因事情应该是这样的,因为 Guido 很少出错。但是,为什么会这样呢? $ cat myModule.py #!/usr/bin/pyt
我们正在将 Rails 3.2 应用程序升级到 Rails 4.0。 我们有一个 assets/stylesheets/application/index.css.sass加载一些其他 sass 文件
我正在开发一个相当小的 Typescript 代码库,该代码库已经足够大,可以拆分到多个文件中。这是一个二十一点游戏。我目前有一堆代码,看起来像: var player = new Player();
是否可以以当模块为 use 时的方式编写模块? d 没有显式导入所有子例程都被导入,当它是 use d 显式导入只有这些显式导入的子程序可用? #!/usr/bin/env perl6 use v6;
这个问题在这里已经有了答案: how to watch changes in whole directory/folder containing many sass files (9 个回答) 5年前
我真的很难让它在 xcode 4 中工作。 我有一个项目将在许多应用程序(网络)中重用,因此我创建一个工作区并添加我的两个项目。到目前为止,一切都很好....这就是失败的地方.. #import "J
经典提取器和新提取器之间的主要区别是什么,哪个最好用? 最佳答案 经典提取器使用原始工作流程,与爬虫和连接器相同。 新的提取器更加精简,通常看起来和感觉都更好,并且经典提取器中的许多小错误已在新提取器
在处理 google webfont import mixin 时,我注意到无法动态构建 @import URL。 .gFontImport (@name, @weights, @subsets) {
我正在关注Django 1.8 tutorial 。在我的项目中mysite ,有一个源文件夹polls 。文件夹中有views.py模块其中 index函数已定义。还有一个urls.py文件: fr
我想使用名为 warp 的第三方库编译一个简单的 Rust 程序: [package] name = "hello-world-warp" version = "0.1.0" [dependencie
我是一名优秀的程序员,十分优秀!