簡體   English   中英

如何在typescript中導入js文件?

[英]How to import js file in typescript?

我想在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'對象。 請給我一個解決方案。 多謝!

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

在您的文件../../pages/mobile.js ,您必須export您的atlan對象( 如果您當然可以編輯此文件 ),然后,您導入它的方式與對所有內容的操作方式相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM