简体   繁体   English

将自我执行功能导入打字稿中的另一个类

[英]import self executing function to another class in typescript

I am doing Hybrid AngularJS/Angular5 application. 我正在做Hybrid AngularJS / Angular5应用程序。 So I am trying step by step change my JavaScript files to Typescript. 因此,我正在尝试逐步将我的JavaScript文件更改为Typescript。 I had javascript file : 我有javascript文件:

(function () {
    'use strict';

    var domainPath = "http://localhost:26264/";
    var reportAPI = "http://localhost:58629/";
    var onlineHelpURL = "http://localhost:8085/";
    var hh6ServiceUrl = "https://localhost:40100/";

    var sysSettings = {
        webServiceURL: domainPath,
        hh6ServiceUrl: hh6ServiceUrl,
        reportServiceURL: reportAPI,
        onlineHelpURL: onlineHelpURL
    };

    angular.module('app.sysSettings', []).constant("sysSettings", sysSettings);
})();  

and I changed it in typescript to be able to export it and reuse settings in my typescript files : 我在打字稿中对其进行了更改,以便能够导出它并重复使用我的打字稿文件中的设置:

declare var angular: any;

let sysSettingsts = (function () {
    'use strict';
    var domainPath = "http://localhost:26264/";
    var reportAPI = "http://localhost:58629/";
    var onlineHelpURL = "http://localhost:8085/";
    var hh6ServiceUrl = "http://localhost:40100/";
    var sysSettings: any = {
        webServiceURL: domainPath,
        hh6ServiceUrl: hh6ServiceUrl,
        reportServiceURL: reportAPI,
        onlineHelpURL: onlineHelpURL
    };

    angular.module('app.sysSettings', []).constant("sysSettings", sysSettings);

    return sysSettings;
})();

export default sysSettingsts;

But when I am trying to import that file : 但是当我尝试导入该文件时:

import { Injectable } from '@angular/core';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';
import {HttpClient} from "@angular/common/http";
import {sysSettingsts} from "angular/sysSettings";

@Injectable()
export class CustomTranslateLoader implements TranslateLoader  {

  constructor(private http: HttpClient,
              private item: sysSettingsts) {
    this.item = item;
  }

  getTranslation(lang: string): Observable<any>{

var apiAddress = this.item.domainPath + "api/GlobalResources/?lang=" + lang;
return Observable.create(observer => {
  this.http.get(apiAddress, ).subscribe(res => {
      observer.next(res);
      observer.complete();
    },
    error => {
      console.log("cannot retrieve Global Resources");
    }
  );
});

} }

I am able to see only values in import {sysSettingsts} from "angular/sysSettings" ; 我只能import {sysSettingsts} from "angular/sysSettings"看到import {sysSettingsts} from "angular/sysSettings"值; file but insight constructor my sysSettingsts is undefined. 文件,但洞察构造函数sysSettingsts未定义。

  constructor(private http: HttpClient,
              private item: sysSettingsts) {
    this.item = item;
  }

I tried to use sysSettingsts directly inside the method but the value is undefined too... Could please anyone give me a hind how can I export executing a function in typescript or at least give some idea how can I import settings from my typescript file in another typescript files (make settings reusable). 我试图直接在方法内部使用sysSettingsts,但该值也未定义...请任何人给我一个后面,我如何导出在打字稿中执行的函数,或者至少给出一些想法如何从打字稿文件中导入设置另一个打字稿文件(使设置可重复使用)。

Thanks 谢谢

Change either your export or import syntax. 更改您的导出或导入语法。 Right now they don't match. 现在他们不匹配。

When using a default export: 使用默认导出时:

export default sysSettingsts;

The corresponding import syntax is: 相应的导入语法为:

import sysSettingsts from "angular/sysSettings"

Note the lack of braces. 请注意缺少括号。


Or, if you want to maintain the same import syntax, then use the regular (non default) export instead: 或者,如果您想要保持相同的导入语法,请改用常规(非默认)导出:

export { sysSettingsts }

You can read more about the import/export pattern here . 您可以在此处阅读有关导入/导出模式的更多信息

The file you want to change doesn't export anything, the purpose of this file is its side effect (it modifies the external angular object). 您要更改的文件不会导出任何内容,该文件的目的是它的副作用 (它修改了外部angular对象)。 You don't want to do anything with a return value from the file, you just want its code to execute. 您不希望对文件的返回值做任何事情,只想执行其代码即可。

Then just import 'your-file.ts'; 然后只需import 'your-file.ts'; and remove the surrounding self-executing function 并删除周围的自执行功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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