Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Module 使用TypeScript进行默认导入_Module_Typescript_Ecmascript 6 - Fatal编程技术网

Module 使用TypeScript进行默认导入

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):错

使用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):错误TS2304:找不到 姓名“Person”。[13:53:43] 类型脚本:2个语义错误


确保您是使用ES6目标编译的,如果我没有弄错的话,ES5仍然是默认目标。

经过一些更改后,它对我有效,在ES5和ES6中进行了测试。我希望能帮助你:

原创的

  • 从“类/人”导入人
  • 从“班级/英国人”中输入英国人

  • 换考

  • 从“/Person”导入人员
  • 从“/classes/Englishman”导入英国人
  • 也许你需要检查你的目录树

  • 从“./classes/Person”导入人员


    • tsc版本1.8.2
    • 节点v5.4.1

    是的,我相信ES5是目标。我没有使用Babel或其他transpiler,因此我认为它应该保持ES5。完美!添加
    /
    有效。但我不知道为什么有必要这样做,因为我认为从当前目录开始是给定的,除非我们从
    /
    开始…奇怪。
    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' }