将JS函数分为使用公共模块的不同文件

发布于 2025-02-11 00:08:54 字数 973 浏览 0 评论 0原文

总结问题:是否有任何方法分发使用通用外部模块的功能,以便我不需要在每个文件中再次下载相同的外部模块。 (在香草JS中)

细节: 你好呀!因此,我一直在使用Firebase 9.6.0进行我的项目(单个JS文件),我用来导入它(Vanilla JS)。但是现在,该项目已经变得更大,并且使用单个JS文件不再有利。我阅读了有关导入和导出功能的信息。因此,我尝试了类似以下内容(main.js被导入HTML文件中的模块):

file:main.js
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-analytics.js";
import { getFirestore, collection, addDoc,arrayRemove, setDoc, updateDoc, getDocs, doc, serverTimestamp, getDoc, } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import { getData } from "module1.js"
file: module1.js
export function getData(){
  try {
      let docSnap = await getDoc(doc(db, "fruit","apple" ))
      if (docSnap.exists()) {
        var docJSON = docSnap.data();
   ....
}

它不再起作用。是因为我没有在另一个模块中导入火箱模块吗?但是,如果这样将它们拆分的情况,那就是意味着我需要再次下载相同的JS模块。那么有什么方法可以解决这个问题吗?

Summarising The Problem: Is there any way of distributing functions which use a common external module into different files such that I don't need to download the same external modules again in each file. (In Vanilla JS)

Detailing:
Hi There! So I have Been Using Firebase 9.6.0 For My Project(single js file) where I used to import it (Vanilla JS). But now the project has become lot bigger and using a single JS file is not favorable anymore. I read about importing and exporting functions. So I tried something like the following (main.js is imported as a module in HTML file):

file:main.js
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-analytics.js";
import { getFirestore, collection, addDoc,arrayRemove, setDoc, updateDoc, getDocs, doc, serverTimestamp, getDoc, } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import { getData } from "module1.js"
file: module1.js
export function getData(){
  try {
      let docSnap = await getDoc(doc(db, "fruit","apple" ))
      if (docSnap.exists()) {
        var docJSON = docSnap.data();
   ....
}

it doesnt work anymore. Is it because i didnt import firebase modules in the other module? But if thats the case splitting them would just mean i need to download the same js modules again an again. So is there any way of fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2025-02-18 00:08:54

您可以下载远程文件并将它们与您的项目一起包装。

因此,您始终需要访问LIB模块,您会在本地找到它们。

You can download the remote files and package them along with your project.

So always you need to access the lib modules, you will find them locally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文