angular5用params加载外部脚本

时间:2017-12-18 09:57:18

标签: angular angular-cli

我需要将下一个脚本加载到我的角度应用

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

据我所知,我可以将全局js放到.angular-cli.json(不适用于外部脚本)或index.html,但是如何添加params(google_key)?

1 个答案:

答案 0 :(得分:1)

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) 
}

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

相关问题