当前位置: 首页 > news >正文

Firebase Local Emulator Suite详解

在这里插入图片描述

文章目录

  • Firebase Local Emulator Suite 组件
  • 安装和使用步骤
    • 1. 安装 Firebase CLI
    • 2. 初始化 Firebase 项目
    • 3. 配置模拟器
    • 4. 启动模拟器
    • 5. 配置应用程序使用本地模拟器
  • 常见用途

Firebase Local Emulator Suite 是一组本地服务,可以模拟 Firebase 平台的在线服务,帮助开发者在本地环境中开发和测试应用程序,而不必依赖实时的 Firebase 项目。以下是关于 Firebase Local Emulator Suite 的详细介绍及其使用方法。

Firebase Local Emulator Suite 组件

  1. Authentication Emulator:模拟 Firebase Authentication,用于测试用户身份验证。
  2. Firestore Emulator:模拟 Cloud Firestore 数据库。
  3. Realtime Database Emulator:模拟 Firebase Realtime Database。
  4. Storage Emulator:模拟 Firebase Storage。
  5. Hosting Emulator:模拟 Firebase Hosting 环境。
  6. Functions Emulator:模拟 Cloud Functions for Firebase。
  7. Pub/Sub Emulator:模拟 Google Cloud Pub/Sub。

安装和使用步骤

1. 安装 Firebase CLI

首先确保安装了 Firebase CLI。如果尚未安装,请运行以下命令:

npm install -g firebase-tools

2. 初始化 Firebase 项目

在项目根目录下运行以下命令:

firebase init

选择要初始化的 Firebase 功能,并确保选择 “Emulators” 以启用本地模拟器。

3. 配置模拟器

在初始化过程中,选择你要使用的模拟器,例如 Firestore、Functions 等。初始化完成后,Firebase 会生成一个 firebase.json 配置文件,其中包含模拟器的配置。

4. 启动模拟器

在项目根目录下运行以下命令以启动所有配置的模拟器:

firebase emulators:start

你将看到类似如下的输出:

✔  All emulators ready! It is now safe to connect your app.┌────────────────┬────────────────────────────────┐
│ Emulator       │ Host:Port                      │
├────────────────┼────────────────────────────────┤
│ Authentication │ localhost:9099                 │
│ Firestore      │ localhost:8080                 │
│ Functions      │ localhost:5001                 │
│ Hosting        │ localhost:5000                 │
│ Pub/Sub        │ localhost:8085                 │
│ Storage        │ localhost:9199                 │
└────────────────┴────────────────────────────────┘┌────────────────┬────────────────────────────────┐
│ View Emulator  │ http://localhost:4000          │
└────────────────┴────────────────────────────────┘

这些是本地模拟器的地址和端口,可以在浏览器中打开 http://localhost:4000 查看模拟器套件的控制面板。

5. 配置应用程序使用本地模拟器

你需要将你的应用程序配置为使用本地模拟器。例如,使用 Firebase SDK 时,可以如下配置 Firestore 和 Authentication:

import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';// 初始化 Firebase
const firebaseConfig = {apiKey: "YOUR_API_KEY",authDomain: "YOUR_PROJECT_ID.firebaseapp.com",projectId: "YOUR_PROJECT_ID",storageBucket: "YOUR_PROJECT_ID.appspot.com",messagingSenderId: "YOUR_MESSAGING_SENDER_ID",appId: "YOUR_APP_ID"
};firebase.initializeApp(firebaseConfig);// 使用本地模拟器
if (window.location.hostname === 'localhost') {firebase.firestore().useEmulator('localhost', 8080);firebase.auth().useEmulator('http://localhost:9099');
}

常见用途

  1. 开发和测试:在本地开发和测试 Firebase 应用程序,无需每次更改都部署到云端。
  2. 集成测试:使用本地模拟器进行端到端集成测试,确保所有组件正常工作。
  3. 离线开发:即使没有网络连接,也能继续开发和测试应用程序。

结束语
Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!

http://www.lryc.cn/news/360519.html

相关文章:

  • 计算机组成原理·存储系统疑点归纳
  • 在 GPU 上实现全规模文件系统加速
  • 代码随想录算法训练营Day7|454.四数相加II、 383. 赎金信、15. 三数之和、 18. 四数之和
  • 编译器屏障概述
  • RUST宏编程入门
  • linux安装srs
  • IO流(2)
  • 【docker】docker启动bitnami/mysql
  • 边缘计算、云计算、雾计算在物联网中的作用
  • 【c语言】探索内存函数
  • day46 完全背包理论基础 518. 零钱兑换 II 377. 组合总和 Ⅳ
  • 【linux】运维-基础知识-认知hahoop周边
  • Python自动实时查询预约网站的剩余名额并在有余额时发邮件提示
  • Flutter 验证码输入框
  • 如何从0到设计一个CRM系统
  • Numba 的 CUDA 示例 (2/4):穿针引线
  • 项目的各个阶段如何编写标准的Git commit消息
  • Python课设-学生信息管理系统
  • openssl 常用命令demo
  • 【Linux】Linux基本指令2
  • springboot+vue+mybatis博物馆售票系统+PPT+论文+讲解+售后
  • java—MyBatis框架
  • 如何使用Spring Cache优化后端接口?
  • 大话C语言:第21篇 数组
  • transfomer中attention为什么要除以根号d_k
  • iperf3带宽压测工具使用
  • [数据集][目标检测]焊接处缺陷检测数据集VOC+YOLO格式3400张8类别
  • 2024华为OD机试真题-剩余银饰的重量-C++(C卷D卷)
  • 糖果促销【百度之星】/思维
  • 【python学习】安装Anaconda后,如何进行环境管理(命令行操作及图形化操作Anaconda Navigator)及包管理