## React-Router相關問題
1. #### 如何使用JS代碼,來動態控制路由的跳轉?
React-Router 返回的組件,屬性里面有 history 變量,實現跳轉的代碼如下
`this.props.history.pushState(null, '/user' ) `
2. #### React-Router二級路由,路由匹配返回的組件問題?
如果項目規模不是太小的話,就會存在一級模塊,二級模塊,亦或是三級模塊。 如 `127.0.0.1:3000/#/user/detail` 這樣的的路由,返回的detail組件,是掛在 一級模塊 user 上的。
那么問題來了,
1. /user路由**有**相對應的組件
2. /user路由**沒有**相對應的組件
**情況1:** `/user/detail` 返回的組件 ,在 **user 對應組件**的 `this.props.children` 屬性中
**情況2:** `/user/detail` 返回的組件,在 **/ 對應的組件**的 `this.props.children` 屬性中
```
//情況1代碼:
module.exports = {
path: 'user',
//子路由
getChildRoutes(location, cb) {
require.ensure([], (require) => {
cb(null, [
require('./routes/test1')
])
})
},
//當前路由返回的控件
getComponent(location, cb) {
require.ensure([], (require) => {
console.log('user', location)
cb(null, require('./components'))
})
}
}
```
```
//情況2代碼:
module.exports = {
path: 'authority',
//子路由
getChildRoutes(location, cb) {
require.ensure([], (require) => {
cb(null, [
require('./authoritysp'),
require('./datapolicy'),
require('./list')
])
})
}
}
```