在Angular2中使用javascript类

在Angular2中使用javascript类,javascript,angular,hl7-fhir,Javascript,Angular,Hl7 Fhir,我想在我的Angular2项目中使用本机javascript。我构建了它并创建了一个捆绑的js文件。现在,我只需要这个文件中的一个名为fhir的类。我试图创建一个新的javascript类,并在其中创建fhir类的实例。然后,我在自己的函数中从该实例调用函数: /// <reference path='./FhirClient.d.ts' /> import { fhir } from './nativeFhir'; export var FhirClient = (functi

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

/// <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
文件

当我试图在
Angular
中导入我的类(
FhirClient
)并从该类的实例调用函数时

    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);
    });
它给出了
。。。不是一个函数
错误:


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

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

您的导出类(是的,您需要在此处输入生命和返回类):

用法:

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

你没发现这个图书馆有打字机吗?也许我错了,但我认为你应该用生命来包装它。当前不执行函数:
export var FhirClient=(function(){…})(
)。你忘了
()
。或者您需要
new FhirClient()()
@VadimB我像您说的那样尝试过,现在错误是
\uuuu网页包\uu导入的模块\uu 5\uuuu FhirJS\u FhirClient\uuu。a不是构造函数
是的,因为您需要参数较少的构造函数。所以这种方法在这里不起作用。在导入
returnfhirclient之后,尝试保持原样并添加return语句以获得正确的共构函数@VadimB我不知道在哪里返回它。你能更清楚地解释一下吗?我试过的最后一个表单是这样的,它给出了
异常:Uncaught(承诺):TypeError:\uuuu webpack\u require\uuuu.I(…)不是函数
main.bundle.js
的错误。当我查看
main.bundle.js
时,这一行是
this.client=\uuuuuuuuu webpack\u require\uuuuuuuuu.I(\uuuuuuuuu webpack\u IMPORTED\u MODULE\u 0\uuu nativeFhir\uuuuuu[“fhir”])(配置)。它应该是
this.client=fhir(配置)js文件中的行。我想问题在于我是如何导入
fhir
类的。你是如何导入这个类的<代码>从“…”导入{FhirClient}”?不,我指的是如何从
nativeFHIR.js
导入
fhir
import{fhir}from./nativeFHIR'否,我的意思是在使用
this.client=newfhirclient(env.environment.server.config)之前
您需要先导入
FhirClient
。在从“文件路径”导入的
import{FhirClient}中是否有此项?是的,我已经导入了它。现在,我从'/nativeFhir'中删除了
import{fhir}FhirClient.js
中的code>行,我将
添加到index.html中。现在它成功了。但是我不想把它导入
index.html
,我想把它变成一个独立的模块。
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);