我们如何在 React(一般情况下)中配置包。
在 NodeJS 中,我们有单例,所以我们可以创建一个外部文件,比如 (./firebase-config.js
),然后将它导入我们的主 Index.js
文件(我们的节点应用程序的起点)。
在 React 中,我习惯于在 Index.html
中配置它,但现在我使用的是 react-native 它没有 DOM 所以我不能把我的配置放在那里。
从哪里获取 index.js
或 app.js
(考虑 cli 版本,其中 index.js 是根,app.js 是具有所有其他 child 的直接 child )我的配置文件夹位于 ./src/config/flamelink.js
,这是我的配置
import * as firebase from 'firebase';
import flamelink from 'flamelink';
const firebaseConfig = {
apiKey: '<your-api-key>', // required
authDomain: '<your-auth-domain>', // required
databaseURL: '<your-database-url>', // required
projectId: '<your-project-id>', // required
storageBucket: '<your-storage-bucket-code>', // required
messagingSenderId: '<your-messenger-id>' // optional
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const app = flamelink({ firebaseApp });
问题: 我应该在哪里导入它来配置它?还是 React-native 会像 node 一样工作并且仅通过导入就可以工作?
最佳答案
是的,您可以只在 firebase-config.js
中进行配置,它可能看起来像
const firebaseConfig = {
apiKey: '<your-api-key>', // required
authDomain: '<your-auth-domain>', // required
databaseURL: '<your-database-url>', // required
projectId: '<your-project-id>', // required
storageBucket: '<your-storage-bucket-code>', // required
messagingSenderId: '<your-messenger-id>' // optional
};
export default firebaseConfig;
然后做
import firebaseConfig from './firebase-config';
在您的代码中的其他地方。
关于javascript - 在 React/React Native 中配置包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019444/