【问题标题】:Reexport class in Typescript在 Typescript 中重新导出类
【发布时间】:2016-04-22 10:10:11
【问题描述】:

我在两个文件中有两个类。

//a.ts
export class A{}

//b.ts
export class B{}

我如何构建文件c.ts 来导入两个类?

import {A, B} from "c";

而不是

import {A} from "a";
import {B} from "b";

我想做一种出口外观。 如何重新导出类型?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    我自己找到了答案

    https://www.typescriptlang.org/docs/handbook/modules.html@Re-exports

    代码做我想做的事

    //c.ts
    export {A} from "a";
    export {B} from "b";
    

    默认导出

    假设你有文件

    //d.ts
    export default class D{}
    

    再出口必须是这样的

    //reexport.ts
    export { default } from "d";
    

    //reexport.ts
    export { default as D } from "d";
    

    这里发生的情况是您说“我想重新导出模块“D”的default export,但名称为D

    【讨论】:

    • 如果AB 是默认(且未命名)导出怎么办? export A from 'a'投诉declaration or statement required
    • @dark_ruby 您必须在单独的语句中导入和导出,更新答案
    【解决方案2】:

    对于不想处理默认值的任何人,您可以重新导出导入的 像这样的模块

    import {A} from './A.js';
    import {B} from './B.js';
    
    const C = 'some variable'
    
    export {A, B, C}
    

    这样您可以导出您在此文件中导入或声明的所有变量。

    取自here

    【讨论】:

    • 如果您不需要此文件中的 A/B:export { A } from './A'; export { B } from './B'; export const C = '...'
    猜你喜欢
    • 2020-09-10
    • 2019-06-06
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2021-01-11
    • 2019-06-18
    • 2021-04-14
    相关资源
    最近更新 更多