私は私の角型2プロジェクトにインポートするために、私は、Typescriptのすべてを書き直すことなく、次のJavascriptファイルを持っています。角2 - TypeError:未定義のプロパティ 'createMessage'を読み取ることができません
すべてが正しく接続されていたとき、私は次のエラーを取得する...
「EXCEPTION:例外TypeError:未定義のプロパティを読み取ることができません 『createMessage』」
ここでは私の、対応するファイルがある...
applicationAPI.js
var myApplication = myApplication || {};
(function(myApplication) {
myApplication.Message = function() {
this.m_messageContents = "New Message";
};
myApplication.Message.prototype.getApplicationMessageContents = function() {
return this.m_messageContents;
};
myApplication.SystemFactory = (function(){
var factory =
{
createMessage: function() {
return new myApplication.Message();
}
};
return factory;
}());
}(myApplication));
applicationAPI.d.ts
declare module "myApplicationAPI" {
export interface Message {
getApplicationMessageContents(): string;
}
export class SystemFactory {
static createMessage(): Message;
}
}
奇妙なことに、applicationAPI.jsが同じapplicationAPI.d.tsファイルで次のようになっていると、これはうまくいきます。
applicationAPI.js
(function() {
this.Message = function() {
this.m_messageContents = "New Message";
};
this.Message.prototype.getApplicationMessageContents = function() {
return this.m_messageContents;
};
this.SystemFactory = (function(){
var factory =
{
createMessage: function() {
return new this.Message();
}
};
return factory;
}());}());
このシナリオが動作するために追加する必要がある他に何上の任意の考え?それはあなたがapplicationAPIの外に何かを輸出していない...呼び出しがどこから来ている。これはある...
home.component.ts
import { Component, OnInit } from '@angular/core';
import * as myApp from "myApplicationAPI";
@Component({
moduleId: module.id,
selector: 'app-home',
templateUrl: 'home.component.html',
styleUrls: ['home.component.css']
})
export class HomeComponent implements OnInit {
title: string;
constructor() {}
ngOnInit() {
this.title = myApp.SystemFactory.createMessage().getApplicationMessageContents();
}
}
どのラインでエラーが表示されますか?あなたが実際に 'SystemFactory.createMessage()'をどこでも呼び出すようには見えません。 –
私の "home.component.ts"ファイルからの呼び出しです。追加しました。後のユースケースではすべてうまく動作しますが、jsに "myApplication"オブジェクトが導入されると、それが壊れます。 – Daz
私の勘違いは、問題が.d.tsファイルにありますが、解決策がわからないことです。 – Daz