相关疑难解决方法(0)

如何在Angular中动态加载外部脚本?

我有这个模块,它将外部库与其他逻辑组件化,而无需将<script>标记直接添加到index.html中:

import 'http://external.com/path/file.js'
//import '../js/file.js'

@Component({
    selector: 'my-app',
    template: `
        <script src="http://iknow.com/this/does/not/work/either/file.js"></script>
        <div>Template</div>`
})
export class MyAppComponent {...}
Run Code Online (Sandbox Code Playgroud)

我注意到importES6规范是静态的,并且在TypeScript转换过程中而不是在运行时解析.

无论如何要使它可配置,以便file.js将从CDN或本地文件夹加载?如何告诉Angular 2动态加载脚本?

javascript typescript ecmascript-6 angular

116
推荐指数
11
解决办法
14万
查看次数

标签 统计

angular ×1

ecmascript-6 ×1

javascript ×1

typescript ×1