![](/img/trans.png)
[英]Why is export from index.tsx file undefined? (React + Typescript)
[英]Why is this Typescript export undefined?
我正在嘗試使用 Typescript 和 React 制作 NPM package。 (多倫多證券交易所)
我正在關注這篇博文,但試圖制作多個組件而不僅僅是一個。
當我嘗試像這樣導入我的模塊時
import { HelloWorld } from 'tsx_lib'
它顯示為未定義
console.log(HelloWorld) //undefined
我的文件夾結構如下所示
src /
GoodbyeWorld.tsx
HelloWorld.tsx
index.ts
index.d.ts
這是文件
再見世界.tsx
import * as React from 'react';
import { GoodbyeWorldProps } from '../index';
export default class GoodbyeWorld extends React.Component<any, any> {
render() {
return <div style={{ color: this.props.color }}>Goodbye world!</div>;
}
}
HelloWorld.tsx
import * as React from 'react';
import { HelloWorldProps } from '../index';
export default class HelloWorld extends React.Component<any, any> {
render() {
return <div style={{ color: this.props.color }}>Hello world!</div>;
}
}
索引.ts
export * from './HelloWorld';
export * from './GoodbyeWorld';
索引.d.ts
import * as React from 'react';
export interface HelloWorldProps extends React.Props<HelloWorld> {
color: string;
}
export interface GoodbyeWorldProps extends React.Props<HelloWorld> {
color: string;
}
declare module 'hello-world' {
}
declare module 'goodbye-world' {
}
export class HelloWorld extends React.Component<HelloWorldProps, any> {}
export class GoodbyeWorld extends React.Component<HelloWorldProps, any> {}
我究竟做錯了什么?
export default class HelloWorld
中的HelloWorld.tsx
是默認導出。
export * from './HelloWorld'
不會通過默認導出。
你有兩個選擇:
HelloWorld.tsx
命名的導出export class HelloWorld extends Component {
...
export { default as HelloWorld } from './HelloWorld'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.