我都尝试过并且都有效。有什么区别?
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/