久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - React-router中的this.props.history.push,url發生了變化,但是頁面沒有變化

瀏覽:136日期:2023-05-14 10:51:16

問題描述

react-router是v4版本,代碼如下

import React, { Component } from ’react’;import { BrowserRouter as Router, Switch, Route, Redirect, withRouter } from ’react-router-dom’;import ’./index.less’;import Work from ’./index/work’;import Info from ’./index/info’;class Index extends Component { constructor(props) {super(props); } handleRouterPush(path, e) {this.props.history.push(path); } render() {return ( <p><Router> <p><Switch> <Route exact path='/index'><Redirect from='/index' to='/index/work' /> </Route> <Route path='/index/work' component={ Work } /> <Route path='/index/info' component={ Info } /></Switch><p className='index-bottom'> <p onClick={ this.handleRouterPush.bind(this, ’/index/work’) }><p className='index-bottom-icon'> <span>工作</span></p> </p> <p onClick={ this.handleRouterPush.bind(this, ’/index/info’) }><p className='index-bottom-icon'> <span>個人</span></p> </p></p> </p></Router> </p>); }}export default withRouter(Index);

若是改成使用Link跳轉則是可以的,但是this.props.history.push就不行了,請問這是為什么?

問題解答

回答1:

我解決了。因為這個組件是在App.js中的Route加載的,我在App.js里面也使用了Router組件,似乎再在index.js里面使用Router組件就重復了。我把index.js里面的Router刪了就好了

回答2:

<Switch> <Route exact path='/index'><Redirect from='/index' to='/index/work' /> </Route> <Route **exact** path='/index/work' component={ Work } /> <Route **exact** path='/index/info' component={ Info } /></Switch>

試試

標簽: JavaScript
主站蜘蛛池模板: 鄂尔多斯市| 富阳市| 靖边县| 阳城县| 昔阳县| 金秀| 杂多县| 西乌| 华安县| 南靖县| 古蔺县| 长治县| 翁源县| 江西省| 英德市| 巴彦淖尔市| 马关县| 安新县| 凌海市| 荥经县| 华池县| 榆中县| 衡东县| 绥江县| 雷波县| 中宁县| 南和县| 麻阳| 于田县| 巴南区| 灵丘县| 郁南县| 酉阳| 吉安市| 亳州市| 航空| 壤塘县| 曲松县| 陕西省| 油尖旺区| 安平县|