在本机 firebase 中调用 firebase.app() 的目的是什么?
What is the purpose of calling firebase.app() in 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.
您不需要调用该方法,除非您在应用程序中使用多个 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.
您不需要调用该方法,除非您在应用程序中使用多个 firebase 应用程序实例