Module 使用TypeScript进行默认导入
使用tsc 1.8.9。。。为什么这些进口商品不起作用?我以为TypeScript实现了ES6模块语法 “类/人.ts” “班级/英国人.ts” “main.ts”Module 使用TypeScript进行默认导入,module,typescript,ecmascript-6,Module,Typescript,Ecmascript 6,使用tsc 1.8.9。。。为什么这些进口商品不起作用?我以为TypeScript实现了ES6模块语法 “类/人.ts” “班级/英国人.ts” “main.ts” import*作为$from“jquery”; 从“班级/英国人”中输入英国人; 让汤姆:人=新英国人(“汤姆”); 控制台日志(tom); $(“body”).html(`TEST`); 错误: source/main.ts(2,24):错误TS2307:找不到模块 “班级/英国人”。source/main.ts(4,10):错
import*作为$from“jquery”;
从“班级/英国人”中输入英国人;
让汤姆:人=新英国人(“汤姆”);
控制台日志(tom);
$(“body”).html(`TEST`);
错误:
source/main.ts(2,24):错误TS2307:找不到模块
“班级/英国人”。source/main.ts(4,10):错误TS2304:找不到
姓名“Person”。[13:53:43]
类型脚本:2个语义错误
确保您是使用ES6目标编译的,如果我没有弄错的话,ES5仍然是默认目标。经过一些更改后,它对我有效,在ES5和ES6中进行了测试。我希望能帮助你: 原创的
从“类/人”导入人代码>
从“班级/英国人”中输入英国人代码>
换考
从“/Person”导入人员代码>
从“/classes/Englishman”导入英国人代码>
从“./classes/Person”导入人员代码>
- tsc版本1.8.2
- 节点v5.4.1
/
有效。但我不知道为什么有必要这样做,因为我认为从当前目录开始是给定的,除非我们从/
开始…奇怪。
export default class Person {
protected _name: string;
protected _language: string;
constructor(name: string) {
this._name = name;
this.hello();
}
public hello() {
console.log("Hello, " + this._name);
console.log("Lang: " + this._language);
}
}
import Person from "person"
export default class Englishman extends Person {
constructor(name: string){
this._language = "en_GB";
super(name);
}
}
import * as $ from "jquery";
import Englishman from "classes/englishman";
let tom: Person = new Englishman("Tom");
console.log(tom);
$("body").html(`<h1>TEST</h1>`);
import Person from './person'; //<-- change
export default class Englishman extends Person {
constructor(name: string){
this._language = "en_GB";
super(name);
}
}
export default class Person {
protected _name: string;
protected _language: string;
constructor(name: string) {
this._name = name;
this.hello();
}
public hello() {
console.log("Hello, " + this._name);
console.log("Lang: " + this._language);
}
}
import Englishman from './classes/englishman'; //<-- change
import Person from './classes/person'; //<-- add
class HelloWorld{
public static main(){
let tom: Person = new Englishman("Tom");
console.log(tom);
}
}
HelloWorld.main();
Hello, Tom
Lang: en_GB
Englishman { _language: 'en_GB', _name: 'Tom' }