javascript - 在 React Native Firebase 中调用 firebase.app() 的目的是什么?

标签 javascript firebase react-native react-native-firebase

我都尝试过并且都有效。有什么区别?

import firebase from 'react-native-firebase';

const defaultApp = firebase.app();

defaultApp.database().ref('foobar').once('value', (snapshot) => {
  // snapshot from default app
});

对比

import firebase from 'react-native-firebase';

firebase.database().ref('foobar').once('value', (snapshot) => {
  // snapshot from default app
});

最佳答案

这两种方法是等效的。第二个仅依赖于一些硬编码的默认值,而第一个则更明确。如果您想(例如)在单​​个应用程序中访问数据库,这一点变得尤其明显。

我们的documentation explains this rather well ,所以我将从那里引用:

In most cases, you will only have to initialize a single, default app. You can access services off of that app in two equivalent ways:

// Initialize the default app
var defaultApp = firebase.initializeApp(defaultAppConfig);

console.log(defaultApp.name);  // "[DEFAULT]"

// You can retrieve services via the defaultApp variable...
var defaultStorage = defaultApp.storage();
var defaultDatabase = defaultApp.database();

// ... or you can use the equivalent shorthand notation
defaultStorage = firebase.storage();
defaultDatabase = firebase.database();

Some use cases require you to create multiple apps at the same time. For example, you might want to read data from the Realtime Database of one Firebase project and store files in another project. Or you might want to authenticate one app while have another app be unauthenticated. The Firebase SDK allows you create multiple apps at the same time, each with their own configuration information.

// Initialize the default app
firebase.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = firebase.initializeApp(otherAppConfig, "other");

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

// Use the shorthand notation to retrieve the default app's services
var defaultStorage = firebase.storage();
var defaultDatabase = firebase.database();

// Use the otherApp variable to retrieve the other app's services
var otherStorage = otherApp.storage();
var otherDatabase = otherApp.database();

Note: Each app instance has its own configuration options and authentication state.

关于javascript - 在 React Native Firebase 中调用 firebase.app() 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556635/

相关文章:

javascript - 未捕获的语法错误 : Unexpected identifier in Object Literal

javascript - 使用 POST 而不是 GET 将数据传递给 objective-c

firebase - 错误 : package com. google.firebase.messaging 不存在

android - 安装 react-native-device-info 错误 android 后

ios - 在 OS X 上使用 ndenv 设置 React Native

javascript - 如何修复 'One Closing Button Closes All Modals'

javascript - 导入JavaScript时如何在QML中使用绝对路径?

android - Firebase @PropertyName 不起作用

firebase - Flutter Web 配置文件隐私

没有开发的 React 原生故事书。服务器