【问题标题】:How to route to an absolute path using react router?如何使用反应路由器路由到绝对路径?
【发布时间】:2019-10-13 12:26:36
【问题描述】:

我有一个 react 应用程序使用 react-router 在页面之间路由。 问题是这样的: 我在以下路线的页面中:http://example.com/my-current-location/ 我想路由到http://example.com/my-next-location/

我尝试使用

this.props.history.push("/my-next-location")

但这需要我去http://example.com/my-current-location/my-next-location/ 这不是我需要的。

this.props.history.push("/my-next-location")

我需要的是一种导航到绝对路径的方法。 当然,

location.replace()

不会这样做,因为我不想刷新整页。 我怎样才能做到这一点?

【问题讨论】:

标签: javascript reactjs react-router


【解决方案1】:

你可以使用<Link />或者这个组件

import React from 'react;
import {Redirect} from "react-router-dom";




/**
 * @param {boolean} props.redirect 
 */

const index = props =>{

return props.redirect ? 
<Redirect to="/my-next-location" />
:<div>
   the components code
</div>

}

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2021-04-20
    • 2020-08-22
    • 2017-04-05
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多