Kei001

Ts+React 中装饰器使用问题

  •  
  •   Kei001 · Apr 15, 2022 · 2381 views
    This topic created in 1500 days ago, the information mentioned may be changed or developed.

    使用 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 截图:

    截图.png

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

    3 replies    2022-04-15 23:34:57 +08:00
    mxT52CRuqR6o5
        1
    mxT52CRuqR6o5  
       Apr 15, 2022 via Android   ❤️ 1
    不用就是了,ts 目前的装饰器实现也和 es 标准不一样,为啥非要用呢(像 angular 里的装饰器也是当注解用的,会在编译时做一些特殊处理)
    Kei001
        2
    Kei001  
    OP
       Apr 15, 2022
    原来 ts 里装饰器的标准不一样,了解了!
    Charrlles
        3
    Charrlles  
       Apr 15, 2022 via iPhone
    Decorator 的提案进 stage 3 了,估计 ts 很快就要跟进改掉,还是继续等等吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2521 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:03 · PVG 00:03 · LAX 09:03 · JFK 12:03
    ♥ Do have faith in what you're doing.