在reactjs中创建Firebase的多个实例

时间:2019-05-25 14:17:59

标签: reactjs firebase

我正在尝试初始化两个不同的firebase应用并导出它们。 我尝试创建一个与此类似的文件,然后将其导出。 我们如何同时有效地创建两个实例? 因为我找不到合适的博客,所以对任何博客的任何引用都会有很大帮助。

对其进行编辑以使其清晰。 我需要听另一个Firebase API.FIREBASE_XYZCONFIG

import API from "../api";
import firebase from "firebase/app";
import "firebase/firestore";


firebase.initializeApp(API.FIREBASE_CONFIG);

const firestore = firebase.firestore();
firestore.settings({ timestampsInSnapshots: true });

export default firestore;

这是在其中一个文件中完成并导出的方式。 我所做的是

import API from "../api";
import firebase from "firebase/app";
import "firebase/firestore";

//import broadcastfirestore from './broadcastindex.js'

const a= firebase.initializeApp(API.FIREBASE_CONFIG);
const b= firebase.initializeApp(API.FIREBASE_BROADCAST_CONFIG,'broadcast');

const firestore = a.firestore();
const firestore2= b.firestore();
firestore.settings({ timestampsInSnapshots: true });

export default firestore;

以上方法是否正确初始化?

1 个答案:

答案 0 :(得分:0)

请参见https://firebase.google.com/docs/web/setup#multiple-projects

// Initialize Firebase with a default Firebase project
firebase.initializeApp(firebaseConfig);

// Initialize Firebase with a second Firebase project
var otherProject = firebase.initializeApp(otherProjectFirebaseConfig, "other");

console.log(firebase.app().name);  // "[DEFAULT]"
console.log(otherProject.name);    // "otherProject"

// Use the shorthand notation to access the default project's Firebase services
var defaultStorage = firebase.storage();
var defaultFirestore = firebase.firestore();

// Use the otherProject variable to access the second project's Firebase services
var otherStorage = otherProject.storage();
var otherFirestore = otherProject.firestore();