如何重定向到/user/dashboard [英] How to redirect to /user/dashboard

查看:71
本文介绍了如何重定向到/user/dashboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录后我必须重定向到 /user/dashboard 页面,但是每次我关闭选项卡并再次打开它时,它都会打开默认主页,即 /.

I have to redirect to the /user/dashboard page after login, but every time I close the tab and open it again it is opening the default home page i.e /.

import React from 'react';
import {isAuthenticated} from '../helpers/auth' 
import {Route, Redirect } from 'react-router-dom';
const UserRoute = ({component: Component, ...rest}) => {
    return(
        <Route 
            {...rest}
            render={(props)=>
                isAuthenticated() && isAuthenticated().role === 0 ? (
                    <Component {...props} />
                ):(
                    <Redirect to="/signin" />
                )
            }
        />
    );
}

导出默认用户路由;

推荐答案

这里是路由设置的一般示例,它会检查Public"、Private"和索引路径:

Here is a general example of route setup which does check for "Public", "Private" and index paths:

export default function Routes() {

  function fromIndex() {
    if (isAuthenticated) { // use your authentication mechanism
        return '/user/dashboard'
    }
    return '/login'
  }

  return (
    <Router history={history}> {/* history not required when using BrowserRouter */}
      <Switch>
        <Redirect exact from="/" to={fromIndex()} />

        {/* publicPaths is an array of paths: [{...} */}
        {publicPaths.map(item => (
          <PublicRoute exact={item.exact} key={item.path} path={item.path}>
            <item.component />
          </PublicRoute>
        ))}

        {privatePaths.map(item => (
          <PrivateRoute exact={item.exact} key={item.path} path={item.path}>
            <item.component />
          </PrivateRoute>
        ))}

        <NotFoundPage />
      </Switch>
    </Router>
  )
}

这里,PrivateRoute 是一个组件,它在通过身份验证时呈现 children,否则重定向回/login".

Here, PrivateRoute is a component which renders the children if authenticated otherwise redirects back to '/login'.

并且 PublicRoute 是一个组件,它在未通过身份验证的情况下呈现 children,否则重定向回/".

And PublicRoute is a component which renders the children if not authenticated otherwise redirects back to "/".

这篇关于如何重定向到/user/dashboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆