【问题标题】:angular 4 _ how can I change dynamically google map api key角度 4 _ 如何动态更改谷歌地图 api 密钥
【发布时间】:2018-03-20 15:42:48
【问题描述】:

我在我的应用程序中使用谷歌地图 API,如下所示:

在 index.html 中使用 api 键导入 url:

<script src="https://maps.googleapis.com/maps/api/js?key=myKey"></script>

并在组件中像这样声明 google:

declare let google: any;

并像这样在组件中使用它:

this.map = new google.maps.Map(document.getElementById('googleMap'), this.mapProp);

如何动态更改 index.html 中存在的 API 密钥?

【问题讨论】:

  • 你真的应该把你的 api 密钥涂掉,这样人们就不会拿走它
  • 不需要使用declare let,使用declare即可。为什么要更改 api 密钥?
  • 这是一个假的 api 密钥,只是为了展示
  • 很高兴知道,很高兴你没有通过它那里
  • 你找到答案了吗?

标签: angular google-maps google-maps-api-3 angular7 angular2-services


【解决方案1】:

如果你真的需要这样做,你可以直接将 script 标签添加到 DOM 中。

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=myKey';
$("#someElement").append( script );

它会在你添加新代码时加载它。

代码基于https://stackoverflow.com/a/611016

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 2013-01-06
    相关资源
    最近更新 更多