javascript - Import av moduler i TypeScript fungerar inte
Jag har 2 TypeScript-filer, den första app.ts-filen är inmatningspunkten och den andra mobiles.ts-filen innehåller modulen med gränssnitt och klasser. Systemet av moduler är CommonJS, en ECMAScript-version ES5 är inställd.
När jag kör appen får jag fel i webbläsaren
ReferenceError: require är inte definierad vid app.js:2
innehållet i filen mobiles.ts
export interface Device {
model: string;
company: string;
displayInfo(): void;
}
export class Smartphone implements Device {
model: string;
company: string;
displayInfo(): void {
console.log(`Smartphone. Model: ${this.model} - Company: ${this.company}`);
}
}
export class Tablet implements Device {
model: string;
company: string;
displayInfo(): void {
console.log(`Tablet. Model: ${this.model} - Company: ${this.company}`);
}
}
innehållet i filen app.ts
import {Smartphone} from "./mobiles";
window.onload = () => {
let mi: Smartphone = new Smartphone();
mi.company = "Xiaomi";
mi.model = "RedMi 4 Pro";
mi.displayInfo();
};
Efter en sammanställning innehållet i filen mobiles.js
"use strict";
var Smartphone = (function () {
function Smartphone() {
}
Smartphone.prototype.displayInfo = function () {
console.log("Smartphone. Model: " + this.model + " - Company: " + this.company);
};
return Smartphone;
}());
exports.Smartphone = Smartphone;
var Tablet = (function () {
function Tablet() {
}
Tablet.prototype.displayInfo = function () {
console.log("Tablet. Model: " + this.model + " - Company: " + this.company);
};
return Tablet;
}());
exports.Tablet = Tablet;
//# sourceMappingURL=mobiles.js.map
Efter en sammanställning innehållet i filen app.js
"use strict";
var mobiles_1 = require("./mobiles"); //Error Get here
window.onload = function () {
var mi = new mobiles_1.Smartphone();
mi.company = "Xiaomi";
mi.model = "RedMi 4 Pro";
mi.displayInfo();
};
//# sourceMappingURL=app.js.map