【问题标题】:Property 'userid' does not exist on type '{ }'. TS2339类型“{}”上不存在属性“userid”。 TS2339
【发布时间】:2021-10-15 11:25:09
【问题描述】:

在我的 react-app 中,当我尝试在路由器中保留参数时,出现以下错误:

Property 'userid' does not exist on type '{}'. TS2339 在编译时抛出并停止进程。解决这个问题的正确方法是什么?

这里是代码:

import { FC } from "react";
import { useHistory, useParams } from "react-router-dom";

const ScreenC: FC = (props) => {
  const history = useHistory();
  const { userid } = useParams(); //here the error exist

  const goBack = () => {
    history.goBack();
  };

  return (
    <div>
      <div>{userid}</div>
      <button onClick={goBack}>Go back </button>
    </div>
  );
};

export default ScreenC;

Live Demo

【问题讨论】:

    标签: reactjs typescript types react-router


    【解决方案1】:

    您可以像这样添加userid 的类型:

    const { userid } = useParams<{userid : string}>()
    

    【讨论】:

    • @Vient,它有效。但这是处理这个问题的正确方法吗?我错过了还是需要添加任何@types - 部分?感谢您的快速重播
    猜你喜欢
    • 2016-03-14
    • 2017-02-25
    • 2021-11-24
    • 2019-01-11
    • 2021-05-08
    • 2020-12-13
    • 2021-08-04
    • 2020-07-01
    • 2021-06-03
    相关资源
    最近更新 更多