0

I want to use the global variable cwic declared in an external js library in the app.component.ts file. The js library in kept in the assets folder as cwic-debug.js. Now to initialize the library and use the cwic variable the statement SystemController method must be called as-

cwic.SystemController.initialize() 

index.html

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

I have tried initializing the cwic library in the following manner-

app.component.ts-

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

But since cwic variable in not recogized it throws up an error and underlines the cwic word in red.

The js library i.e. cwic-debug.js looks something like this-

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

How can I use this cwic variable in app.component.ts file?

1 Answer 1

1

You can declare the variable and type it as any.

declare var cwic: any;

TypeScript will then stop to complain about the type of the variable and just assume it exists, but you will no longer get type helps from your IDE.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! It helped! But is there any other way around of importing the cwic variable into the app.component.ts file?
I don't exaclty know what cwic is, but if the library is just injected into the JS domain, probably not. You can look up if the library provides typescript typing files (*.d.ts), which just contain type annotations for typescript, you could then import those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.