我在BMW.ts定義的命名BMW類,如下所示:打字稿內部模塊使用
///<reference path="../Thing.ts"/>
module Entities.Cars {
import e = Entities;
export class BMW extends Vehicle {
public series: string;
constructor (model : string, series : string) {
super("BMW", model)
this.series = series;
}
drive() {
alert("driving a bimmer is a different kind of feeling");
}
toString() : string
{
return this.getName() + " " + this.series + " " + this.getType();
}
}
}
在另一文件Thing.ts,我有限定車輛與物類,如下所示:
module Entities {
// Class
export class Thing {
private _name: string;
private _type: string;
// Constructor
constructor (public name: string, public type: string) {
this._name = name;
this._type = type;
}
getName(): string { return this._name; }
setName(name: string) { this._name = name; }
getType(): string { return this._type; }
setType(name: string) {
this._type = name;
}
toString() : string
{
return "Entities.Thing";
}
}
export class Vehicle extends Thing {
public cargoCapacity: number;
public fuelType: string;
public owner: string;
constructor (make: string, model : string) {
super(make, model)
}
drive() {
}
toString(): string {
return "Entities.Vehicle";
}
}
}
當我試圖引用的事情和寶馬打字稿文件後執行以下代碼:
var car = new Entities.Cars.BMW("335i", "E90");
car.drive();
我得到一個異常以下錯誤「Microsoft JScript運行時錯誤:無法獲取屬性'BMW'的值:對象爲null或未定義」。爲寶馬生成的Javascript有一個錯誤。我的上面的代碼段出了什麼問題?
就是這樣,謝謝。我沒有在app.js中引用Thing.js和BMW.js。現在,TypeScript編譯器在我的項目文件中配置如下:'"%(fullpath )"','')「IgnoreExitCode =」true「/>。我是否將整個事件更改爲tsc --out app.js app.ts? –
傳遞編譯器選項-out scripts \ app.js scripts \ app。我看到一個包含所有代碼的單個Javascript文件。但是,如果我所引用的是app.js,我仍然可以獲取實體undefined ... –
不知道以前發生了什麼。選項1現在適合我。再次感謝。 –