typescript ReferenceError

时间:2015-07-29 02:46:38

标签: javascript visual-studio-2013 typescript referenceerror

Debugger listening on port 5858
c:\workspace\Projects\test\test\Car.js:16
})(Vehicle);
   ^
ReferenceError: Vehicle is not defined
    at Object.<anonymous> (c:\workspace\test\test\Car.js:16:4)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.runMain [as _onTimeout] (module.js:501:10)
    at Timer.listOnTimeout (timers.js:119:15)

我正在使用带有typescript插件的Visual Studio 2013。我收到了这个错误,我尝试了导入和导出,但这两个都没有。我该如何解决?

源:

Car.ts

class Car extends Vehicle {
  public talk() { console.log ("vehicle"); }
}
var rx_car = new Car("");
rx_car.say(); 

Vehicle.ts

class Vehicle {
  private _name: string;
  constructor(name: string) { this._name = name; }
  public say (subject = "name is") { console.log(subject + " " + this._name); }
  get name() { return this._name; }
  set name(name: string) {this._name = name; }
}

编译文件:

Car.js

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var Car = (function (_super) {
    __extends(Car, _super);
    function Car() {
        _super.apply(this, arguments);
    }
    Car.prototype.talk = function () {
        console.log("vehicle");
    };
    return Car;
})(Vehicle);
var rx_car = new Car("");
rx_car.say();
//# sourceMappingURL=Car.js.map

Car.js.map

{"version":3,"file":"Car.js","sourceRoot":"","sources":["Car.ts"],"names":["Car","Car.constructor","Car.talk"],"mappings":";;;;;;AAAA,IAAM,GAAG;IAASA,UAAZA,GAAGA,UAAgBA;IAAzBA,SAAMA,GAAGA;QAASC,8BAAOA;IAEzBA,CAACA;IADQD,kBAAIA,GAAXA;QAAgBE,OAAOA,CAACA,GAAGA,CAAEA,SAASA,CAACA,CAACA;IAACA,CAACA;IAC5CF,UAACA;AAADA,CAACA,AAFD,EAAkB,OAAO,EAExB;AACD,IAAI,MAAM,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC;AACzB,MAAM,CAAC,GAAG,EAAE,CAAC"}

Vehicle.js

var Vehicle = (function () {
    function Vehicle(name) {
        this._name = name;
    }
    Vehicle.prototype.say = function (subject) {
        if (subject === void 0) { subject = "name is"; }
        console.log(subject + " " + this._name);
    };
    Object.defineProperty(Vehicle.prototype, "name", {
        get: function () {
            return this._name;
        },
        set: function (name) {
            this._name = name;
        },
        enumerable: true,
        configurable: true
    });
    return Vehicle;
})();
//# sourceMappingURL=Vehicle.js.map

Vehicle.js.map

{"version":3,"file":"Vehicle.js","sourceRoot":"","sources":["Vehicle.ts"],"names":["Vehicle","Vehicle.constructor","Vehicle.say","Vehicle.name"],"mappings":"AAAA,IAAM,OAAO;IAEXA,SAFIA,OAAOA,CAECA,IAAYA;QAAIC,IAAIA,CAACA,KAAKA,GAAGA,IAAIA,CAACA;IAACA,CAACA;IACzCD,qBAAGA,GAAVA,UAAYA,OAAmBA;QAAnBE,uBAAmBA,GAAnBA,mBAAmBA;QAAIA,OAAOA,CAACA,GAAGA,CAACA,OAAOA,GAAGA,GAAGA,GAAGA,IAAIA,CAACA,KAAKA,CAACA,CAACA;IAACA,CAACA;IAC7EF,sBAAIA,yBAAIA;aAARA;YAAaG,MAAMA,CAACA,IAAIA,CAACA,KAAKA,CAACA;QAACA,CAACA;aACjCH,UAASA,IAAYA;YAAGG,IAAIA,CAACA,KAAKA,GAAGA,IAAIA,CAACA;QAACA,CAACA;;;OADXH;IAEnCA,cAACA;AAADA,CAACA,AAND,IAMC"}

1 个答案:

答案 0 :(得分:3)

由于您在节点上运行,请使用import/require aka外部模块

即。 export Vehicleimport {Vehicle} from "./Vehicle"合并,并使用--module commonjs

进行汇编

更多:http://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html