如何以角度动态加载外部脚本?我有一个模块,它将外部库与附加逻辑一起组件化,而不添加<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 {...}我注意到importby ES6规范是静态的,在类型转换过程中解析,而不是在运行时。无论如何,为了使其可配置,文件js将从CDN或本地文件夹加载吗?如何告诉角2动态加载脚本?
3 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
System.import()
export class MyAppComponent {
constructor(){
System.import('path/to/your/module').then(refToLoadedModule => {
refToLoadedModule.someFunction();
}
);}require.ensure :
export class MyAppComponent {
constructor() {
require.ensure(['path/to/your/module'], require => {
let yourModule = require('path/to/your/module');
yourModule.someFunction();
});
}}添加回答
举报
0/150
提交
取消
