【问题标题】:How to include script tag while Angular2 running ?如何在 Angular2 运行时包含脚本标签?
【发布时间】:2016-12-13 23:45:43
【问题描述】:

如何在 Angular2 页面视图中包含这个外部 java 脚本库。

我想加入这个

 <!-- google maps javascript -->
 <script src="//maps.googleapis.com/maps/api/js?key=#######&sensor=true"></script>

所以当这个页面在视图中时,这个库被加载并开始工作。

【问题讨论】:

标签: angular


【解决方案1】:

您不能。
出于安全原因,Angular 处理器会删除模板文件中的所有脚本标记。


替代方案:

  • 将您的脚本放入index.html
  • 使用import 'path/to/file' 将其直接导入到您的组件.ts 文件中。

【讨论】:

  • 如何在运行时包含 googlmaps 脚本?
  • 为什么不import '//maps.googleapis.com/maps/api/js?key=#######&amp;sensor=true'? (在需要脚本的组件中,因此该组件需要其依赖项)
  • 看看 Angular 如何告诉我们包含 RxJs 并使用相同的方法。 angular.io/docs/ts/latest/guide/…JSFiddle 或 CodePen 不太好展示 Angular2 应用示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 2023-01-08
相关资源
最近更新 更多