react项目,如何去掉http://127.0.0.1:6600/#/中的#号

import BasicRouter from './routes';

class RouterContainer extends React.Component {

    constructor(props) {
        super();

        this.state = {
            textHelp: props.text
        }
    }

    render () {
        return (
            <div>
                <BasicRouter />
            </div>

        );
    }
}

export default RouterContainer;
const BasicRouter = () => (

    <Router history={hashHistory}>

        {/* 登陆 */}
        <Route path='/' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/index').default);
                },'User');
            }}

        />

        <Route path='/register' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/register').default);
                },'Register');
            }} />

        <Route path='/login' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/login').default);
                },'Login');
            }} />

        <Route path='/regExp' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/create-regular-expression').default);
                },'CreateRegularExpression');
            }} />

        <Route path='/shopcart' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/shopcart').default);
                },'Shopcart');
            }} />

        {/* 电信3期 - 大屏 */}

        <Route path='/tel' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/telecom').default);
                },'Telecommunication');
            }} />

        <Route path='/ct' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/telecom/components/contain-task').default);
                },'ContainTask');
            }} />

        <Route path='/st' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/telecom/components/separate-task/fetch-promise-async').default);
                },'SeparateTask');
            }} />


        {/* 新疆 - 智能感知运维大屏 */}
        <Route path='/per' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/perception').default);
                },'Perception');
            }} />

        {/* 新疆 - 运行态势智能大屏 */}
        <Route path='/sit' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/situation').default);
                },'Situation');
            }} />

        <Route path='/manage' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/manage-redux').default);
                },'ManageRedux');
            }} />

        <Route path='/manageSystem' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../../slides/user/modules/ManageSystem').default);
                },'ManageSystem');
            }} />


        {/* 其他重定向到 404 */}
        <Route path='/404' getComponent={
            (nextState,callback)=>{
                require.ensure([],(require)=>{
                    callback(null,require('../404').default);
                },'NotFound');
            }} />

        {/* 其他重定向到 404 */}
        <Redirect from='*' to='/404'/>
    </Router>

);

export default BasicRouter;
阅读 6.6k
4 个回答

把hashHistory,换成browserHistory就行了。

import createBrowserHistory from 'history/createBrowserHistory'
const history = createBrowserHistory()
<Router history={history}>

...
</Router>

<Router history={browserHistory}>

其次还有两点注意:
1.web容器的静态资源记得配置重定向,以Nginx为例,在location中配置try_files $uri $uri/ /index.html;
2.index.html 的head中配置base标签,即

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <base href="/">
  </head>

使用BrowserRouter

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏