我有 config.json 文件,我已经处理了所有的 url 和密钥。有一个名为“googleMapsApiKey”的参数,其中有我的“地图键”。我想将此密钥提供给我的模块文件,以便我可以使用谷歌地图,但我无法提供它。下面是我的代码。
配置文件
{
"googleMapsApiKey": "MY KEY",
}
移动内容模块.ts
import { ConfigService } from '../../../HiP-CmsAngularApp/app/config.service';
@NgModule({
imports: [
AgmCoreModule.forRoot({
apiKey: 'WANT TO PROVIDE THAT KEY HERE', ---> from config.json
libraries: ['places']
}),
CommonModule,
FormsModule,
有一项名为 ConfigService 的服务,我在其中访问我的 config.json 文件。我已经在 MobileContentModule.ts 文件中导入了这个服务。
谁能帮我从 config.json 文件中获取我的 google api 密钥?这样做的目的是,我们不想在 github 上公开这个密钥。
将它导入到您的文件中,如下所示:
import data from './config.json';
[...]
AgmCoreModule.forRoot({
apiKey: data.googleMapsApiKey
libraries: ['places']
}),[...]
但是TypeScript 2+
会抱怨Cannot find module 'config.json'
。然后你必须使用通配符声明,如
declare module "json!*" {
const value: any;
export default value;
}
(参见https://www.typescriptlang.org/docs/handbook/modules.html#wildcard-module-declarations)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句