typescript 如何在打字稿中导入js文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44951112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:41:41  来源:igfitidea点击:

How to import js file in typescript?

javascriptangulartypescriptionic2

提问by So Yeon Shin

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.

我想在打字稿中导入 js 文件。我想访问 js 文件中的对象和函数。我还在 index.html 中添加了 js 文件,但它也不起作用。所以我找到了“导入‘[js 文件路径]’”的线索,但它不起作用。

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!

这是success.ts 文件。我想找到“atlan”对象。请给我一个解决方案。多谢!

回答by Eduardo Dennis

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

您必须使用declare关键字,以免出现任何编译错误。您可以执行以下操作

    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 {
    ....
}

回答by sjahan

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

在您的文件中../../pages/mobile.js,您必须export拥有 atlan 对象(当然如果您可以编辑此文件),然后,您可以像处理所有内容一样导入它。