国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:146日期: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
主站蜘蛛池模板: 富平县| 财经| 贵德县| 政和县| 会宁县| 启东市| 宣威市| 潜江市| 宽城| 蓬安县| 翼城县| 兴国县| 根河市| 仙游县| 佛坪县| 新竹市| 永和县| 南部县| 陇南市| 高碑店市| 乐昌市| 武汉市| 义马市| 喀喇沁旗| 济阳县| 汉阴县| 元氏县| 皋兰县| 白山市| 和平县| 区。| 南雄市| 兴国县| 郑州市| 广安市| 和林格尔县| 娄烦县| 剑阁县| 祁阳县| 比如县| 安乡县|