我正在使用
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router": "^4.1.2",
Hare my rout:
<BrowserRouter>
<div>
<Route exact path='/' component={Layout}></Route>
<Route path='/about' name="about" component={About}>
<Route path="/:article" component={anotherAbout}></Route>
</Route>
<Route path='/protfolio' name="protfolio" component={Portfolio}></Route>
</div>
</BrowserRouter >
当我致电{this.props.match.params.article}
时,它会提供未定义
为什么staticContext: undefined
和我的props.match.params
null对象。
答案 0 :(得分:3)
从react-router v4文档中查看此basic example。
查看const Topics = ({ match }) => ( ... )
,您将看到此组件底部的嵌套路由。这是您在react-router v4中嵌套路由的方法。如果你解决了这个问题,你的问题应该得到解决。
import React from 'react'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'
const BasicExample = () => (
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/topics">Topics</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/topics" component={Topics}/>
</div>
</Router>
)
const Home = () => (
<div>
<h2>Home</h2>
</div>
)
const About = () => (
<div>
<h2>About</h2>
</div>
)
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${match.url}/rendering`}>
Rendering with React
</Link>
</li>
<li>
<Link to={`${match.url}/components`}>
Components
</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>
Props v. State
</Link>
</li>
</ul>
<Route path={`${match.url}/:topicId`} component={Topic}/>
<Route exact path={match.url} render={() => (
<h3>Please select a topic.</h3>
)}/>
</div>
)
const Topic = ({ match }) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
)
export default BasicExample
答案 1 :(得分:1)
我认为这是由于路由嵌套造成的。
而不是使用嵌套的<BrowserRouter>
<div>
<Route exact path='/' component={Layout}></Route>
<Route path='/about' name="about" component={About}></Route>
<Route path="/about/:article" component={anotherAbout}></Route>
<Route path='/protfolio' name="protfolio" component={Portfolio}></Route>
</div>
</BrowserRouter >
并解构匹配的url参数
const { article } = this.props.match.params
我认为这可行