Ts+React 中装饰器使用问题

2022-04-15 10:25:50 +08:00
 Kei001

使用 withRouter 给组件添加路由属性和函数,感觉装饰器的语法更优雅,但是 ts 下 withRouter 装饰暴露组件会出现 ts 校验错误。代码如下:


import React, { Component } from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';

type Props = {} & RouteComponentProps;
type State = {};

@withRouter
export default class Header extends Component<Props, State> {
  render() {
    console.log('Header 组件的 Props:', this.props);
    return <h1>This is Header</h1>;
  }
}

IDE 截图:

看编辑器提示,似乎是一个 issue ,让使用 withRouter()函数调用模式,感觉有点不死心,目前没有很好的解决方案了吗?

1377 次点击
所在节点    TypeScript
3 条回复
mxT52CRuqR6o5
2022-04-15 10:32:45 +08:00
不用就是了,ts 目前的装饰器实现也和 es 标准不一样,为啥非要用呢(像 angular 里的装饰器也是当注解用的,会在编译时做一些特殊处理)
Kei001
2022-04-15 14:07:52 +08:00
原来 ts 里装饰器的标准不一样,了解了!
Charrlles
2022-04-15 23:34:57 +08:00
Decorator 的提案进 stage 3 了,估计 ts 很快就要跟进改掉,还是继续等等吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/847088

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX