繁体   English   中英

为什么这个 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'不会通过默认导出。

你有两个选择:

  1. HelloWorld.tsx命名的导出
export class HelloWorld extends Component { 
...
  1. map 默认导出到命名导出
export { default as HelloWorld } from './HelloWorld'

暂无
暂无

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

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