【问题标题】:How to import js file in typescript?如何在打字稿中导入js文件?
【发布时间】:2017-12-10 14:11:19
【问题描述】:

我想在 typescript 中导入 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);

 }
}

这是success.ts 文件。我想找到'atlan'对象。 请给我一个解决方案。非常感谢!

【问题讨论】:

标签: javascript angular typescript ionic2


【解决方案1】:

您必须使用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 {
    ....
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 1970-01-01
      • 2022-06-14
      • 2023-01-26
      • 2014-05-15
      • 2017-10-22
      • 2017-11-24
      • 2021-02-17
      • 1970-01-01
      相关资源
      最近更新 更多