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

用一个变量接收文件路径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)
        })
    }

你可能感兴趣的:(如何在angular4的组件里面引入外部js文件)