首页 > 解决方案 > 如何在 React Native 中全局导入一些东西?

问题描述

我正在使用 React Native 和 Firebase 开发一个应用程序。因此,我创建了一个单独的js文件来存储configfirebase database然后在需要连接时将该config文件导入到我的文件中screenfirebase database文件中。

所以,我必须将该config文件导入到screen我需要与我的firebase database.

我想知道,是否可以导入该config文件globally

我的意思是,我创建了一个单独的config文件,然后import它只在特定js文件中一次(仅举个例子,让我们想,import它只在App.js文件中一次),然后,screen每当我需要连接到我的firebase database. 但是在这种情况下,是否可以在config不将其导入每个文件的情况下使用该screen文件?

希望你能明白我想做什么。

标签: javascriptreact-native

解决方案


是的,您只需将文件导入主屏幕/应用程序入口点。

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 中的所有功能将适用于所有屏幕。

演示


推荐阅读