相关疑难解决方法(0)

React:“重定向”未从“react-router-dom”导出

npm run start在终端中运行时出现以下错误。

尝试导入错误:“重定向”未从“react-router-dom”导出。

我已经重新安装了node_modules,,react-router-domreact-router还重新启动了终端和我的计算机,但问题仍然存在。

我的代码:

import React from 'react';
import { Switch, Redirect } from 'react-router-dom';

import { RouteWithLayout } from './components';
import { Minimal as MinimalLayout } from './layouts';

import {
  Login as LoginView,
  Dashboard as DashboardView,
  NotFound as NotFoundView
} from './views';

const Routes = () => {
  return (
    <Switch>
      <Redirect
        exact
        from="/"
        to="/dashboard"
      />
      <RouteWithLayout
        component={routeProps => <LoginView {...routeProps} data={data} />}
        exact
        layout={MinimalLayout}
        path="/login"
      />
      <Redirect to="/not-found" /> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router react-router-dom

255
推荐指数
11
解决办法
28万
查看次数

使用React Router重定向页面的最佳方法是什么?

我是React Router的新手,了解到有很多方法可以重定向页面:

  1. 运用 browserHistory.push("/path")

    import { browserHistory } from 'react-router';
    //do something...
    browserHistory.push("/path");
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运用 this.context.router.push("/path")

    class Foo extends React.Component {
        constructor(props, context) {
            super(props, context);
            //do something...
        }
        redirect() {
            this.context.router.push("/path")
        }
    }
    
    Foo.contextTypes = {
        router: React.PropTypes.object
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在React Router v4中,有this.context.history.push("/path")this.props.history.push("/path").详细信息:如何在React Router v4中推送到历史记录?

我对所有这些选项感到困惑,有没有最好的方法来重定向页面?

reactjs react-router redux

68
推荐指数
3
解决办法
11万
查看次数