我正在尝试在 AGM MAPS 中动态加载地图键。我从 API Enpoint 获取地图密钥,下面是我的代码
在 app.module.ts
@Injectable()
export class GoogleMapsConfig implements LazyMapsAPILoaderConfigLiteral {
apiKey;
libraries = ['places']
constructor(private http: HttpClient) {
http.get<any>(`https://us-central1-calvincare-emailservice.cloudfunctions.net/mapkey`).pipe(
map(response => {
this.apiKey = response['mapkey'];
console.log(response['mapkey'])
return response['mapkey'];
})
).toPromise();
}
}
在@NgModule
providers: [{ provide: LAZY_MAPS_API_CONFIG, useClass: GoogleMapsConfig }]
如果我给出静态值,则地图正在加载this.apiKey = 'xxx'
,但是当从 http 请求加载时,地图键没有被加载。
但是当我控制台时,console.log(response['mapkey'])
我可以看到地图键。
我怎样才能解决这个问题 ?请指导