簡體   English   中英

在angular4中的app.component.ts文件中使用外部庫變量

[英]Using external library variable in app.component.ts file in angular4

我想使用在app.component.ts文件的外部js庫中聲明的全局變量cwic js庫以cwic-debug.js的形式保存在資產文件夾中。 現在要初始化庫並使用cwic變量,必須將語句SystemController方法稱為-

cwic.SystemController.initialize() 

的index.html

<script type="text/javascript" src="./assets/cwic-debug.js"></script>

我嘗試以以下方式初始化cwic庫-

app.component.ts-

 export class AppComponent implements AfterViewInit{
  ngAfterViewInit(){
    cwic.SystemController.initialize();
    console.log(cwic);
  }
  title = 'Angular Project';
}

但是由於cwic變量未識別,因此會引發錯誤並用紅色強調cwic單詞。

js庫,即cwic-debug.js看起來像這樣-

var cwic =
/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/

如何在app.component.ts文件中使用此cwic變量?

您可以聲明變量並將其鍵入為any

declare var cwic: any;

然后,TypeScript將停止抱怨變量的類型,並假設它存在,但是您將不再從IDE中獲得類型幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM