打字稿导出与默认导出

export和之间的Typescript有什么区别default export。在所有教程中,我看到人们在写export他们的课程,并且如果我default在导出之前未添加关键字,则无法编译我的代码。


另外,我在官方打字稿文档中找不到默认export关键字的任何痕迹。


export class MyClass {


  collection = [1,2,3];


}

不编译。但:


export default class MyClass {


  collection = [1,2,3];


}

有。


错误是: error TS1192: Module '"src/app/MyClass"' has no default export.


DIEA
浏览 448回答 3
3回答

Cats萌萌

这是简单对象导出的示例。var MyScreen = {    /* ... */    width : function (percent){        return window.innerWidth / 100 * percent    }    height : function (percent){        return window.innerHeight / 100 * percent    }};export default MyScreen在主文件中(不需要和不需要创建新实例时使用),并且它不是全局的,只有在需要时才导入它:import MyScreen from "./module/screen";console.log( MyScreen.width(100) );
打开App,查看更多内容
随时随地看视频慕课网APP