สําหรับเว็บแอป Firebase ส่วนใหญ่ เราขอแนะนําอย่างยิ่งให้ใช้SDK ผ่าน npm อย่างไรก็ตาม Firebase มีวิธีอื่นๆ ในการเพิ่ม SDK ให้กับผู้ใช้ที่มีข้อกําหนดพิเศษ หน้านี้แสดงวิธีการตั้งค่าโดยละเอียดสำหรับวิธีอื่นๆ เหล่านี้
- CDN (เครือข่ายนำส่งข้อมูล)
- npm สําหรับแอป Node.js
วิธีการเหล่านี้ช่วยให้คุณเพิ่มคลังที่มีอยู่ลงในแอปได้
จาก CDN
คุณสามารถกําหนดค่าการนําเข้าบางส่วนของ Firebase JavaScript SDK และโหลดเฉพาะผลิตภัณฑ์ Firebase ที่ต้องการ Firebase จัดเก็บไลบรารีแต่ละรายการของ Firebase JavaScript SDK ใน CDN (เครือข่ายนำส่งข้อมูล) ทั่วโลก
หากต้องการรวมเฉพาะผลิตภัณฑ์ Firebase ที่เฉพาะเจาะจง (เช่น Authentication และ Cloud Firestore) ให้เพิ่มสคริปต์ต่อไปนี้ที่ด้านล่างของแท็ก
<body>
ก่อนใช้บริการ Firebase<body> <!-- Insert this script at the bottom of the HTML, but before you use any Firebase services --> <script type="module"> import { initializeApp } from 'https://www.gstatic.com/firebasejs/11.3.0/firebase-app.js' // If you enabled Analytics in your project, add the Firebase SDK for Google Analytics import { getAnalytics } from 'https://www.gstatic.com/firebasejs/11.3.0/firebase-analytics.js' // Add Firebase products that you want to use import { getAuth } from 'https://www.gstatic.com/firebasejs/11.3.0/firebase-auth.js' import { getFirestore } from 'https://www.gstatic.com/firebasejs/11.3.0/firebase-firestore.js' </script> </body>
เพิ่มออบเจ็กต์การกําหนดค่า Firebase แล้วเริ่มต้นใช้งาน Firebase ในแอป
<body> <script type="module"> // ... // TODO: Replace the following with your app's Firebase project configuration const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); </script> </body>
แอป Node.js
ติดตั้ง Firebase JavaScript SDK โดยทำดังนี้
หากยังไม่มีไฟล์
package.json
ให้สร้างไฟล์โดยเรียกใช้คําสั่งต่อไปนี้จากรูทของโปรเจ็กต์ JavaScriptnpm init
ติดตั้ง
firebase
แพ็กเกจ npm และบันทึกลงในไฟล์package.json
โดยเรียกใช้คำสั่งต่อไปนี้npm install --save firebase@11.3.0
ใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้เพื่อใช้โมดูล Firebase ในแอป
คุณสามารถ
require
โมดูลจากไฟล์ JavaScript ใดก็ได้หากต้องการรวมเฉพาะผลิตภัณฑ์ Firebase ที่เฉพาะเจาะจง (เช่น Authentication และ Cloud Firestore) ให้ทำดังนี้
// Firebase App (the core Firebase SDK) is always required and // must be listed before other Firebase SDKs var firebase = require("firebase/app"); // Add the Firebase products that you want to use require("firebase/auth"); require("firebase/firestore");
คุณใช้โมดูล ES2015 ถึง
import
ได้หากต้องการรวมเฉพาะผลิตภัณฑ์ Firebase ที่เฉพาะเจาะจง (เช่น Authentication และ Cloud Firestore) ให้ทำดังนี้
// Firebase App (the core Firebase SDK) is always required and // must be listed before other Firebase SDKs import firebase from "firebase/app"; // Add the Firebase services that you want to use import "firebase/auth"; import "firebase/firestore";
เพิ่มออบเจ็กต์การกําหนดค่า Firebase แล้วเริ่มต้นใช้งาน Firebase ในแอป
import { initializeApp } from 'firebase/app'; // TODO: Replace the following with your app's Firebase project configuration const firebaseConfig = { //... }; // Initialize Firebase const app = initializeApp(firebaseConfig);