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

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:145日期: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跳轉(zhuǎn)則是可以的,但是this.props.history.push就不行了,請問這是為什么?

問題解答

回答1:

我解決了。因為這個組件是在App.js中的Route加載的,我在App.js里面也使用了Router組件,似乎再在index.js里面使用Router組件就重復(fù)了。我把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
主站蜘蛛池模板: 闽侯县| 什邡市| 滦南县| 林西县| 调兵山市| 济阳县| 永福县| 阳信县| 灵丘县| 河东区| 鄯善县| 常山县| 女性| 南溪县| 昌江| 安福县| 邵东县| 余庆县| 鄂托克前旗| 石泉县| 昌江| 津南区| 娄烦县| 平定县| 温州市| 邳州市| 河曲县| 北票市| 沙雅县| 建水县| 泗水县| 左云县| 八宿县| 青州市| 霍邱县| 腾冲县| 高安市| 榕江县| 吴桥县| 揭阳市| 永靖县|