加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 在TypeScript中声明react-router参数的内容

发布时间:2020-12-15 05:05:24 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Typescript以一种接受参数的方式设置react-router. 在我的渲染元素中我有 Route path="/show/:id" component={TestComp} / 我将TestComp定义为 const TestComp = ({ match }) = ( div h2Showing specified ID: {match.params.id}/h2 /div) 但
我正在尝试使用Typescript以一种接受参数的方式设置react-router.

在我的渲染元素中我有

<Route path="/show/:id" component={TestComp} />

我将TestComp定义为

const TestComp = ({ match }) => (
    <div>
        <h2>Showing specified ID: {match.params.id}</h2>
    </div>
)

但是,VS Code会强调match参数(在TestComp的声明中)并告诉我

Binding element ‘match’ implicitly has an ‘any’ type.

它无法编译.

任何人都可以告诉我应该声明什么类型匹配?我尝试过RouteProps,但这也不起作用.查看index.d.ts,我认为它被定义为匹配< P>但我不确定如何将参数声明为泛型类型.

UPDATE
根据对@ TarasPolovyi的回答的评论,我添加了以下内容:

如您所见,这仍然存在问题.

如果您正在使用react-router v4,则从react-router-dom导入RouteComponentProps并使用类型RouteComponentProps< RouteInfo> – 参数名称必须匹配

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读