Angular.js专场angular2与vue的那些事Angular开发指南

如何在angular4的组件里面引入外部js文件

2018-03-09  本文已影响35人  阿踏

用一个变量接收文件路径userFunctionJS: any="../../assets/js/index.js";

    ngAfterViewInit(){
        // alert($('#yjfkxh').html());
     if (this.userFunctionJS){
          this.loadScript(this.userFunctionJS);      
        }else{
          console.log("userFunctionJS=" + this.userFunctionJS);
        }
      }
      loadScript(scriptUrl: string) {
        return new Promise((resolve, reject) => {
        console.log("loadScript:" + scriptUrl);
        const scriptElement = document.createElement('script')
        scriptElement.src = scriptUrl
        scriptElement.onload = resolve
        document.body.appendChild(scriptElement)
        })
    }
上一篇下一篇

猜你喜欢

热点阅读