npm run start在终端中运行时出现以下错误。
尝试导入错误:“重定向”未从“react-router-dom”导出。
我已经重新安装了node_modules,,react-router-dom。react-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) 我是React Router的新手,了解到有很多方法可以重定向页面:
运用 browserHistory.push("/path")
import { browserHistory } from 'react-router';
//do something...
browserHistory.push("/path");
Run Code Online (Sandbox Code Playgroud)运用 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)在React Router v4中,有this.context.history.push("/path")和this.props.history.push("/path").详细信息:如何在React Router v4中推送到历史记录?
我对所有这些选项感到困惑,有没有最好的方法来重定向页面?