在 Angular2 中使用 javascript class

Using javascript class in Angular2

我想在我的 Angular2 项目中使用本机 javascript library。我构建了它并创建了一个捆绑的 js 文件。现在,我只需要此文件中一个名为 fhir 的 class。我试图创建一个新的 javascript class 并在其中创建 fhir class 的实例。然后,我在自己的函数中从该实例调用函数:

/// <reference path='./FhirClient.d.ts' />

import { fhir } from './nativeFhir';

export var FhirClient = (function() {

    function FhirClient(config) {
        this.client = fhir(config);
    }

    FhirClient.prototype.conformance = function(query) {
        return this.client.conformance(query);
    };
    FhirClient.prototype.document = function(query) {
        return this.client.document(query);
    };
    FhirClient.prototype.profile = function(query) {
        return this.client.profile(query);
    };
    FhirClient.prototype.transaction = function(query) {
        return this.client.transaction(query);
    };
    FhirClient.prototype.history = function(query) {
        return this.client.history(query);
    };
    FhirClient.prototype.typeHistory = function(query) {
        return this.client.typeHistory(query);
    };
    FhirClient.prototype.resourceHistory = function(query) {
        return this.client.resourceHistory(query);
    };
    FhirClient.prototype.read = function(query) {
        return this.client.read(query);
    };
    FhirClient.prototype.vread = function(query) {
        return this.client.vread(query);
    };
    FhirClient.prototype.delete = function(query) {
        return this.client.delete(query);
    };
    FhirClient.prototype.create = function(query) {
        return this.client.create(query);
    };
    FhirClient.prototype.validate = function(query) {
        return this.client.validate(query);
    };
    FhirClient.prototype.search = function(query) {
        return this.client.search(query);
    };
    FhirClient.prototype.update = function(query) {
        return this.client.update(query);
    };
    FhirClient.prototype.nextPage = function(query) {
        return this.client.nextPage(query);
    };
    FhirClient.prototype.prevPage = function(query) {
        return this.client.prevPage(query);
    };
    FhirClient.prototype.resolve = function(query) {
        return this.client.resolve(query);
    };

});

我为此创建了一个 .d.ts 文件 class:

import { fhir } from './nativeFHIR';

export declare class FhirClient {
    private client: fhir;

    constructor(config: any);

    conformance(query: any): any;
    document(query: any): any;
    profile(query: any): any;
    transaction(query: any): any;
    history(query: any): any;
    typeHistory(query: any): any;
    resourceHistory(query: any): any;
    read(query: any): any;
    vread(query: any): any;
    delete(query: any): any;
    create(query: any): any;
    validate(query: any): any;
    search(query: any): any;
    update(query: any): any;
    nextPage(query: any): any;
    prevPage(query: any): any;
    resolve(query: any): any;

}

但是,我没有为我正在使用的库编写 .d.ts 文件。

当我尝试在 Angular 中导入我的 class (FhirClient) 并从 class 的实例调用函数时,

    this.client = new FhirClient(env.environment.server.config);
    this.client.search({type: 'Patient', id: 'pa000001'}).then(function (response) {
      console.log(response);
    }, function (error) {
      console.log(error);
    });

它给出 ... is not a function 错误:

这是我第一次尝试使用 .d.ts 文件,可能我完全用错了。你能帮我在我的 Angular 项目中使用本地库中的 fhir class 吗?

试试这个解决方案,因为它适用于我的情况:

您导出的class(是的,您需要此处的 IIFE 和 return class 最后):

import { fhir } from './nativeFhir';

export var FhirClient = (function() {

    function FhirClient(config) {
        this.client = fhir(config);
    }

    FhirClient.prototype.conformance = function(query) {
        return this.client.conformance(query);
    };
    FhirClient.prototype.document = function(query) {
        return this.client.document(query);
    };
    FhirClient.prototype.profile = function(query) {
        return this.client.profile(query);
    };
    FhirClient.prototype.transaction = function(query) {
        return this.client.transaction(query);
    };
    FhirClient.prototype.history = function(query) {
        return this.client.history(query);
    };
    FhirClient.prototype.typeHistory = function(query) {
        return this.client.typeHistory(query);
    };
    FhirClient.prototype.resourceHistory = function(query) {
        return this.client.resourceHistory(query);
    };
    FhirClient.prototype.read = function(query) {
        return this.client.read(query);
    };
    FhirClient.prototype.vread = function(query) {
        return this.client.vread(query);
    };
    FhirClient.prototype.delete = function(query) {
        return this.client.delete(query);
    };
    FhirClient.prototype.create = function(query) {
        return this.client.create(query);
    };
    FhirClient.prototype.validate = function(query) {
        return this.client.validate(query);
    };
    FhirClient.prototype.search = function(query) {
        return this.client.search(query);
    };
    FhirClient.prototype.update = function(query) {
        return this.client.update(query);
    };
    FhirClient.prototype.nextPage = function(query) {
        return this.client.nextPage(query);
    };
    FhirClient.prototype.prevPage = function(query) {
        return this.client.prevPage(query);
    };
    FhirClient.prototype.resolve = function(query) {
        return this.client.resolve(query);
    };

    return FhirClient;
})();

用法:

this.client = new FhirClient(env.environment.server.config);