我想将此密钥导出到app.js,但无法执行,显示此错误:-
"const"只能在.ts文件中使用。
ApiKey.js
import React from 'react';
const firebaseConfig;
export default class firekeys extends React.Component {
const firebaseConfig ={
apiKey: "key",
authDomain: "auth",
databaseURL: "url",
projectId: "Id",
storageBucket: "",
messagingSenderId: "SID"
}
};
App.js
import React from 'react';
import firekeys from "./constants/ApiKeys"
import * as firebase from 'firebase';
firebase.initializeApp(firekeys.firebaseConfig);
我在这里看到的两个问题:1.不能重新声明const
。2.您导出的是您的类,而不是const
。此外,您应该将类属性分配给React类,而不是变量。以下内容应该有效,但您需要将firebaseConfig
作为命名导出导入。
export const firebaseConfig ={
apiKey: "key",
authDomain: "auth",
databaseURL: "url",
projectId: "Id",
storageBucket: "",
messagingSenderId: "SID"
}
export default class firekeys extends React.Component {
};
您应该按照如下方式重写ApiKeys.js,除非您有一些特定的原因需要在组件中使用它们:
export default firebaseConfig = {
apiKey: "key",
authDomain: "auth",
databaseURL: "url",
projectId: "Id",
storageBucket: "",
messagingSenderId: "SID"
}
import firekeys from "./constants/ApiKeys"
(公平地说,你给它们命名的变量并不重要)
然后你应该能够访问密钥如下
firekeys.apiKey
您可能应该了解使用let、var和const的不同方法https://medium.com/podiihq/javascript-variables-should-you-use-let-var-or-const-394f7645c88f