一个模块在Reactjs > TypeScript中不能有多个默认导出。

huangapple go评论123阅读模式
英文:

A module cannot have multiple default exports in reactjs > typescript

问题

我有以下的项目树

src
 Components
  Create
   Create.tsx
   SuccessPage.tsx
   index.ts
 index.ts
index.ts

Create > index.ts

export { default } from "./Create";
export { default } from "./SuccessPage"; // 这里是错误

Component > index.ts

export { default as Create } from "./Create";

Src > index.ts

export * from './components';

我想要导出两个页面(create.tsx 和 success.tsx)
注意这两个页面包含两个类

class Create extends React.Component<IProps, IState> {
.....
}
export default Create;

class Success extends React.Component<IProps, IState> {
.....
}
export default Success;

我该如何做到这一点?

英文:

I have the following project tree

src
 Components
  Create
   Create.tsx
   SuccessPage.tsx
   index.ts
 index.ts
index.ts

Create > index.ts

export { default } from &quot;./Create&quot;;
export { default } from &quot;./SuccessPage&quot;; // here is the error

Componenet > index.ts

export { default as Create } from &quot;./Create&quot;;

Src > index.ts

export * from &#39;./components&#39;;

I want to export both pages ( create.tsx and success.tsx)
note that the pages contains 2 class

class Create extends React.Component&lt;IProps, IState&gt; {
.....
}
export default Create;

and

class Success extends React.Component&lt;IProps, IState&gt; {
.....
}
export default Success;

how can i do that?

答案1

得分: 0

将它们更改为具名导出,这样您可以拥有多个:

export { default as Create } from "./Create";
export { default as Success } from "./SuccessPage";

或者,您可以将其中一个保留为默认导出,但我怀疑这可能不是您真正想要的。

export { default } from "./Create";
export { default as Success } from "./SuccessPage";
英文:

Change them to named exports, so you can have multiple:

export { default as Create } from &quot;./Create&quot;;
export { default as Success } from &quot;./SuccessPage&quot;;

Alternatively, you could keep one of them as the default export, but I doubt that's really what you want.

export { default } from &quot;./Create&quot;;
export { default as Success } from &quot;./SuccessPage&quot;;

huangapple
  • 本文由 发表于 2023年3月7日 22:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定