简体   繁体   中英

How to import javascript file into angular4 project and use it's function inside component?

I am trying to import openseadragon.min.js file in index.html and using it's function like below,

var viewer = OpenSeadragon({
  id: "seadragon-viewer"
});

But it is throwing error like OpenSeadragon is undefined .

Any solutions will be appreciated, Thank you

Try declaring the variable at the top of your component immediately below your imports, not inside your class export statement.

import { Component } from '@angular/core';

declare var OpenSeadragon: any;

@Component({
...
})

You need angular typings to do this correctly for OpenSeadragon. You also shouldn't be using vars in typescript as it breaks context and causes scope and hoisting issues.

Please see https://github.com/alvaromartmart/types-openseadragon for typings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM