属性'背景'在类型上不存在,未知' ReactJS/usehistory()

发布于 2025-01-26 04:40:33 字数 420 浏览 1 评论 0原文

我在类型的'unknown.ts(2339)上获取此错误属性'背景',当我尝试使用此代码时,

const location = useLocation();
const background = location.state && location.state.background;

我从React-Router-dom导入了所有所需的函数

import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  useLocation,
  withRouter,
} from "react-router-dom";

,我正在使用类Component如果确实很重要。

如果您能给我一些技巧,我将非常感激。

I'm getting this error Property 'background' does not exist on type 'unknown'.ts(2339), when I try to use this code

const location = useLocation();
const background = location.state && location.state.background;

I imported all of the needed functions from react-router-dom

import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  useLocation,
  withRouter,
} from "react-router-dom";

I'm using class component if it does matter.

I will be very grateful if you could give me some tips.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2025-02-02 04:40:33
import {Location} from "history";
const location = useLocation<{background?: Location<{} | null | undefined>}>();
import {Location} from "history";
const location = useLocation<{background?: Location<{} | null | undefined>}>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文