如何用React Router的history实现长尾词页面的跳转?

2026-04-09 06:321阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计325个文字,预计阅读时间需要2分钟。

如何用React Router的history实现长尾词页面的跳转?

在React Router中,组件内部的跳转可以使用`Link`组件,但若要在组件外部跳转,则需要使用React Router的`history`对象的`replace`或`push`方法。这两种方法的使用形式相同,`replace`的作用是替换当前历史记录,而`push`则是添加新的历史记录。

javascripthistory.replace(path); // 替代当前历史记录history.push(path); // 添加新的历史记录

在react-router中组件里面的跳转可以用<Link>

但是在组件外面改如何跳转,需要用到react路由的history

如何用React Router的history实现长尾词页面的跳转?

replace方法和push方法使用形式一样,replace的作用是取代当前历史记录
go,此方法用来前进或者倒退,history.go(-1);
goBack,此方法用来回退,history.goBack();
goForward,此方法用来前进,history.goForward();

1.hook

import {useHistory} from 'react-router-dom'; function goPage(e) { history.push({ pathname: "/home", state: {id: 1} }); }

pathname是路由地址,state可以传参

获取参数:

import {useLocation} from 'react-router-dom'; function getParams(){ let location = useLocation(); let id = location.state.id; }

2.class组件

import React from 'react'; import {createBrowserHistory} from "history"; class App extends React.Component{ constructor(props) { super(props); } goPage() { let history = createBrowserHistory() history.push({ pathname: "/home", state: {id: 1} }); history.go(); } render() {return null;} }

如果不调用history.go则路由改变了,但是页面不会跳转。

到此这篇关于React Router 如何使用history跳转的实现的文章就介绍到这了,更多相关React Router history跳转内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计325个文字,预计阅读时间需要2分钟。

如何用React Router的history实现长尾词页面的跳转?

在React Router中,组件内部的跳转可以使用`Link`组件,但若要在组件外部跳转,则需要使用React Router的`history`对象的`replace`或`push`方法。这两种方法的使用形式相同,`replace`的作用是替换当前历史记录,而`push`则是添加新的历史记录。

javascripthistory.replace(path); // 替代当前历史记录history.push(path); // 添加新的历史记录

在react-router中组件里面的跳转可以用<Link>

但是在组件外面改如何跳转,需要用到react路由的history

如何用React Router的history实现长尾词页面的跳转?

replace方法和push方法使用形式一样,replace的作用是取代当前历史记录
go,此方法用来前进或者倒退,history.go(-1);
goBack,此方法用来回退,history.goBack();
goForward,此方法用来前进,history.goForward();

1.hook

import {useHistory} from 'react-router-dom'; function goPage(e) { history.push({ pathname: "/home", state: {id: 1} }); }

pathname是路由地址,state可以传参

获取参数:

import {useLocation} from 'react-router-dom'; function getParams(){ let location = useLocation(); let id = location.state.id; }

2.class组件

import React from 'react'; import {createBrowserHistory} from "history"; class App extends React.Component{ constructor(props) { super(props); } goPage() { let history = createBrowserHistory() history.push({ pathname: "/home", state: {id: 1} }); history.go(); } render() {return null;} }

如果不调用history.go则路由改变了,但是页面不会跳转。

到此这篇关于React Router 如何使用history跳转的实现的文章就介绍到这了,更多相关React Router history跳转内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!