javascript 在Firebase CDN脚本/模块标记中使用外部JS文件

kmbjn2e3  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(121)

我想在我需要导入的firebase模块中使用外部JavaScript脚本...
例如:

<script type="module">

  // Import the functions you need from the SDKs you need    
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js";    
  import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-analytics.js";

  // TODO: Add SDKs for Firebase products that you want to use    
  // https://firebase.google.com/docs/web/setup#available-libraries    

  // Your web app's Firebase configuration    
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional    
  const firebaseConfig = {    
    apiKey: "AIzaSyA4G1l7vDgwzXoe0RGGX327EuG_c5nsm44",    
    authDomain: "pga222.firebaseapp.com",    
    databaseURL: "https://pga222-default-rtdb.europe-west1.firebasedatabase.app",    
    projectId: "pga222",    
    storageBucket: "pga222.appspot.com",    
    messagingSenderId: "233815977986",    
    appId: "1:233815977986:web:77093639624930a3ab371b",    
    measurementId: "G-QC3N2GJRC6"    
  };    

  // Initialize Firebase    
  const app = initializeApp(firebaseConfig);    
  const analytics = getAnalytics(app);    

<script type="text/javascript" src="path-to-javascript-file.js"></script>

</script>

这不起作用,我认为它不起作用是因为你不能在脚本中调用脚本。
我整个网站上的所有脚本都是在Firebase模块脚本中编写的,有这么多乱七八糟的代码要看,真是令人沮丧。
这将是容易得多,如果我可以分离成块与外部脚本文件一样,你通常可以不使用Firebase。
有没有办法绕过这个-不使用webpack ect?或者这正是webpack的用途?

isr3a4wc

isr3a4wc1#

你正在使用的语法是无效的
为什么不把firebase config打包成一个文件,然后像这样导入它:
firebase.js

// Import the functions you need from the SDKs you need    
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js";    
  import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-analytics.js";

  // TODO: Add SDKs for Firebase products that you want to use    
  // https://firebase.google.com/docs/web/setup#available-libraries    

  // Your web app's Firebase configuration    
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional    
  const firebaseConfig = {    
    apiKey: "AIzaSyA4G1l7vDgwzXoe0RGGX327EuG_c5nsm44",    
    authDomain: "pga222.firebaseapp.com",    
    databaseURL: "https://pga222-default-rtdb.europe-west1.firebasedatabase.app",    
    projectId: "pga222",    
    storageBucket: "pga222.appspot.com",    
    messagingSenderId: "233815977986",    
    appId: "1:233815977986:web:77093639624930a3ab371b",    
    measurementId: "G-QC3N2GJRC6"    
  };    

  // Initialize Firebase    
  const app = initializeApp(firebaseConfig);    
  const analytics = getAnalytics(app);  

export { app, analytics }
<script type="module">
import { app, analytis } from "./firebase.js"

// content file path-to-javascript-file.js
</script>

相关问题