2016-07-24 86 views
0

就像标题所说,我该如何防止在url结尾添加#。如何防止反应路由器添加#到url?

routes.jsx

(export default (withHistory, onUpdate) => { 
const history = withHistory? 
       (Modernizr.history ? 
       browserHistory 
       : hashHistory) 
      : null; 
return (
<Router history={history} onUpdate={onUpdate}> 
    <Route path='/' component={App}> 
    <IndexRoute component={quotes} /> 
    <Route path='/app/search' component={SimpleSearch} /> 

    <Route path='pathName' component={Blank1} /> 
     <Route path='pathName/:category' component={Blank1}/> 
     <Route path='pathName/:category(/:page)' component={Blank1}/> 
    <Route path='app/:id' component={Blank1} /> 

    </Route> 

</Router> 

); 
}; 

方法blank1.jsx

method(current,previous){ 
    const category= this.props.params.category; 
    const page = current; 
    const path = `/pathName/${category}/${page}`; 
    browserHistory.push(path); 
... 
} 

我试过很多东西,但无法得到无#的链接。 它将#添加到最后并持续触发componentWillReceiveProps,在此处使用this.setState更改状态。并且还通过没有导航项目的导航是因为#

+1

你应该使用browserHistory获得没有哈希 –

回答

0

你是指clean URLS不活跃。

你想用browserHistory代替hasHistory/

然后您需要添加一个*路线在你的服务器,让所有的请求被路由通过路由器作出反应。

像这样的事情在你的服务器路线的结尾:

app.get('*', function (req, res) { 
    res.sendFile(path.join(__dirname, 'public', 'index.html')) 
}); 

欲了解更多信息结帐this example

+0

网址在我server.js我有 应用。 get('*',function(req,res,next){renderIdResq(req,res,next); }); –