gpt4 book ai didi

javascript - typescript 引用错误

转载 作者:行者123 更新时间:2023-11-28 19:06:00 26 4
gpt4 key购买 nike

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();

车辆.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"}

车辆.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"}

最佳答案

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

导出Vehicleimport {Vehicle} from "./Vehicle"结合使用--module commonjs

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

关于javascript - typescript 引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com