angular5使用参数加载外部脚本

angular5使用参数加载外部脚本,angular,angular-cli,Angular,Angular Cli,我需要将下一个脚本加载到我的angular应用程序 <script src="https://maps.googleapis.com/maps/api/js?key={{google_key}}&libraries=places&language=DE_de"></script> 据我所知,我可以将全局js放入。angular-cli.json不适用于外部脚本或index.html,但如何添加参数google_key?index.html中动态脚本的更快

我需要将下一个脚本加载到我的angular应用程序

<script src="https://maps.googleapis.com/maps/api/js?key={{google_key}}&libraries=places&language=DE_de"></script>

据我所知,我可以将全局js放入。angular-cli.json不适用于外部脚本或index.html,但如何添加参数google_key?

index.html中动态脚本的更快解决方案是通过id引用它,并从app.component.ts设置src属性:

googleApi = `https://maps.googleapis.com/maps/api/js?key=
 ${environment.google_key}&libraries=places&language=DE_de`

constructor(@Inject(DOCUMENT) private document: any) {}

ngOnInit() {
  this.document.getElementById('theId').setAttribute('src', this.googleApi) 
}

但是,正如评论中所建议的,在这种情况下,将其作为构建过程的一部分可能是一个更好的解决方案。

在应用程序的整个生命周期中,您的谷歌密钥是否会发生变化?我认为params适合动态内容,在用户呈现前端之前,这些内容可能会经常更改或未知。对于google api密钥,我建议最好在构建过程中替换该密钥,而不是在运行时,例如,将其放置在index.html中,并在通过script@BenediktSchmidt这不是动态的-对。你有没有建议像吞咽/咕噜声这样的解决方案?你是在通过这种方式进行建设吗?我目前正在做类似的事情,我只是在angular cli构建之后运行一个脚本来更新我的index.html。我在npm作业中结合调用,它可能看起来像ng build&node\u yourHtmlUpdateFile\u@BenediktSchmidt谢谢,这应该可以工作,但我仍然在寻找更漂亮的东西,这是默认要求,angular cli应该支持它,而不需要hacksthanks,它也可以工作,但我认为应该有一些解决方案,从盒子没有黑客