How to import js file in typescript?

23,600

Solution 1

You have to use the declare keyword so you do not get any compilation errors. You can do the following

    import { Component } from '@angular/core';
     ....
    /* Here you are telling typescript compiler to 
       ignore this variable it did not originate with typescript.*/
    declare var atlan: any; 

    @Component({
      selector: 'page-success',
      templateUrl: 'success.html'
    })

export class SuccessPage {
    ....
}

Solution 2

In your file ../../pages/mobile.js, you must export your atlan object (if you can edit this file of course), then, you import it the same way you do with everything.

Share:
23,600
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to import js file in typescript. And I want to access object and function in the js files. Also I added js file in index.html but It doesn't working too. so I find a clue that "import '[js file path]'" but it doesn't working.

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { NavParams } from 'ionic-angular';
    import '../../pages/mobile.js';
    
    
    @Component({
      selector: 'page-success',
      templateUrl: 'success.html'
    })
    
    export class SuccessPage {
      constructor(public navCtrl: NavController, public navParms: NavParams) {
    
    let centerPos =  new atlan.maps.UTMK(953933.75, 1952050.75);
    
     }
    }
    

    This is success.ts file. I want to find 'atlan' object. Give me a solution please. Thx a lot!