反应路由器 - 未定义的历史记录

Nie*_*and 6 javascript ecmascript-6 reactjs react-router

我试图使用1.0.0-rc1 react-router和历史2.0.0-rc1按下按钮后手动浏览网站.不幸的是,按下按钮后我得到:

无法读取未定义的属性'pushState'

我的路由器代码:

import React from 'react';
import { Router, Route, Link, browserHistory } from 'react-router'
import AppContainer from './components/AppContainer.jsx';
import MyTab from './components/test/MyTab.jsx';
import MainTab from './components/test/MainTab.jsx';


var routes = (
    <Route component={AppContainer} >
        <Route name="maintab" path="/" component={MainTab} />
        <Route name="mytab" path="/mytab" component={MyTab} />
    </Route>
);

React.render(<Router history={browserHistory}>{routes}</Router>, document.getElementById('main'));
Run Code Online (Sandbox Code Playgroud)

导航按钮在MyTab上,它尝试导航到MainTab:

import React from 'react';
import 'datejs';
import History from "history";
export default React.createClass({

    mixins: [ History ],

    onChange(state) {
        this.setState(state);
    },

    handleClick() {
        this.history.pushState(null, `/`)
    },

    render() {
        return (
            <div className='container-fluid' >
                <button type="button" onClick={this.handleClick.bind(this)}>TEST</button>
            </div>

        );
    }
});
Run Code Online (Sandbox Code Playgroud)

当我使用历史时,this.props.history一切正常.这段代码有什么问题?

编辑.

添加以下内容后:

const history = createBrowserHistory();

React.render(<Router history={history}>{routes}</Router>, document.getElementById('main'));
Run Code Online (Sandbox Code Playgroud)

我尝试访问我的应用程序.之前(没有history = {history}),我刚刚访问过localhost:8080/testapp,一切正常 - 我的静态资源被生成到dist/testapp目录中.现在在这个URL下我得到:

位置"/ testapp /"与任何资源都不匹配

我尝试以下列方式使用useBasename函数:

    import { useBasename } from 'history'
    const history = useBasename(createBrowserHistory)({
    basename: '/testapp'
});

React.render(<Router history={history}>{routes}</Router>, document.getElementById('main'));
Run Code Online (Sandbox Code Playgroud)

应用程序又回来了,但我又得到了错误

无法读取未定义的属性'pushState'

在通话中:

handleClick() {
    this.history.pushState(null, `/mytab`)
},
Run Code Online (Sandbox Code Playgroud)

我认为这可能是因为我的gulp连接任务,所以我添加了history-api-fallback配置:

settings: {
      root: './dist/',
      host: 'localhost',
      port: 8080,
      livereload: {
        port: 35929
      },
      middleware: function(connect, opt){
        return [historyApiFallback({})];
      }
    }
Run Code Online (Sandbox Code Playgroud)

但是在添加中间件之后我访问网站后得到的是:

不能获取 /

小智 5

"react-router": "^4.1.1" 开始,您可以尝试以下操作:

使用' this.props.history.push('/new-route') '。这是一个详细的例子

1:Index.js

 import { BrowserRouter, Route, Switch } from 'react-router-dom'; 
 //more imports here
    ReactDOM.render(
      <div>
        <BrowserRouter>
           <Switch>
              <Route path='/login' component={LoginScreen} />
              <Route path='/' component={WelcomeScreen} />
           </Switch>
        </BrowserRouter>
     </div>, document.querySelector('.container'));
Run Code Online (Sandbox Code Playgroud)

上面,我们使用了来自“react-router-dom”的 BrowserRouter、Route 和 Switch。

所以每当你在 React Router 'Route' 中添加一个组件时,也就是,

<Route path='/login' component={LoginScreen} />
Run Code Online (Sandbox Code Playgroud)

..然后'React Router' 将向这个组件(在本例中为LoginScreen)添加一个名为'history' 的新属性。您可以使用此历史道具以编程方式导航到其他路线。

所以现在在 LoginScreen 组件中,您可以像这样导航:

2:登录屏幕:

return (
      <div>
       <h1> Login </h1>
       <form onSubmit={this.formSubmit.bind(this)} >
         //your form here
       </form>
      </div>

    );



formSubmit(values) {
 // some form handling action
   this.props.history.push('/'); //navigating to Welcome Screen
}
Run Code Online (Sandbox Code Playgroud)