javascript - 如何在 React Native 中全局导入一些东西?
问题描述
我正在使用 React Native 和 Firebase 开发一个应用程序。因此,我创建了一个单独的js
文件来存储config
,firebase database
然后在需要连接时将该config
文件导入到我的文件中screen
firebase database
文件中。
所以,我必须将该config
文件导入到screen
我需要与我的firebase database
.
我想知道,是否可以导入该config
文件globally
?
我的意思是,我创建了一个单独的config
文件,然后import
它只在特定js
文件中一次(仅举个例子,让我们想,import
它只在App.js
文件中一次),然后,screen
每当我需要连接到我的firebase database
. 但是在这种情况下,是否可以在config
不将其导入每个文件的情况下使用该screen
文件?
希望你能明白我想做什么。
解决方案
是的,您只需将文件导入主屏幕/应用程序入口点。
import app from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
import 'firebase/storage';
const config = {
apiKey: "API_KEY",
authDomain: "DOMAIN",
databaseURL: "DB_URL",
projectId: "PROJECT_ID",
storageBucket: "BUCKET",
messagingSenderId: "MSG_ID",
appId: "APP_ID"
};
app.initializeApp(config);
module.Store = {
Firebase() {
return app;
},
}
if (global) {
global.Store = module.Store;
}
在您的主屏幕中
import './Global';
Global 中的所有功能将适用于所有屏幕。
推荐阅读
- .net - Parallel.ForEach 添加和读取ConcurrentBag,安全吗?
- javascript - 编写脚本来更新重复任务
- azure - 如何使用 Azure Logic App 将数据库结果/记录存储到内存缓存中
- php - 此路由不支持 GET 方法。支持的方法:POST。在 Laravel 7.24.0
- reactjs - 如何访问 useEffect 中的 URL 参数?
- clickhouse - 如何在 Clickhouse 中将列从一个表添加到另一个表?
- xamarin.forms - Xamarin 表单:XamForms.Controls.Calendar 中的特殊日期选择问题
- python - 编码中0和-0的区别
- javascript - Firebase 云功能:更新用户配置文件
- apache-curator - Apache Curator 连接状态侦听器并不总是通过 RECONNECTED 状态更改调用