我尝试了所有工作。有什么区别?
import firebase from 'react-native-firebase';
const defaultApp = firebase.app();
defaultApp.database().ref('foobar').once('value', (snapshot) => {
// snapshot from default app
});
VS
import firebase from 'react-native-firebase';
firebase.database().ref('foobar').once('value', (snapshot) => {
// snapshot from default app
});
答案 0 :(得分:4)
这两种方法是等价的。第二个只依赖于一些硬编码的默认值,而第一个更明确。如果您希望(例如)访问单个应用程序中的数据库,这一点就变得尤为明显。
我们的documentation explains this rather well,所以我会从那里引用:
在大多数情况下,您只需初始化一个默认应用。您可以通过两种等效方式访问该应用程序的服务:
// 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();
某些用例要求您同时创建多个应用。例如,您可能希望从一个Firebase项目的实时数据库中读取数据并将文件存储在另一个项目中。或者您可能希望对另一个应用程序进行身份验证,同时让其他应用程序未经身份验证。 Firebase SDK允许您同时创建多个应用程序,每个应用程序都有自己的配置信息。
// 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();
注意:每个应用实例都有自己的配置选项和身份验证状态。
答案 1 :(得分:0)
除非您在应用程序中使用多个firebase应用程序实例,否则无需调用该方法