我有以下Javascript文件,我想导入到我的Angular 2项目中,而无需在Typescript中重新编写整个内容。Angular 2 - TypeError:无法读取未定义的属性'createMessage'
当一切都正确绑起来,我得到以下错误...
“EXCEPTION:类型错误:未定义无法读取属性‘的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;
}());}());
上需要添加别的什么,这种情况下的工作有什么想法?这不是明摆着给我...
这是呼叫来自哪里...
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