简体   繁体   English

无法从打字稿文件导入导出的类

[英]Cannot import an exported class from a typescript file

I have a class exported from a typescript file called foo.ts 我有一个从名为foo.ts的打字稿文件中导出的类

export default class Foo{
}

And I'm trying to import it in another file in the same directory 我正在尝试将其导入同一目录中的另一个文件中

import {Foo} from './foo';

Which gives me an error 这给我一个错误

Module '"foo"' has no exported member 'Foo'.

I've looked around online to solve this, but closest I've got is this github issue https://github.com/Microsoft/TypeScript/issues/16475 which is still unresolved at the time of writing. 我已经在网上环顾四周以解决此问题,但最接近的是这个github问题https://github.com/Microsoft/TypeScript/issues/16475 ,在撰写本文时仍未解决。

Am I missing something obvious here? 我在这里错过明显的东西吗? Shouldn't this be valid code? 这不是有效的代码吗?

These are the options to export/import things: 这些是导出/导入事物的选项:

Default Export 默认导出

// foo.js
export default class Foo{
}
// bar.js
import Foo from './foo'

Named Export 命名为出口

// foo.js
export class Foo{
}
// bar.js
import {Foo} from './foo'

Have a look at the above and you can see that you've mixed the two approaches in the wrong way. 看看上面的内容,您会发现您以错误的方式混合了两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM