简体   繁体   English

如何为 React 项目实现深层链接?

[英]How to implement deeplinks for react project?

I was trying to implement deep links(not sure if this is the correct term) in my react project what I want to do is this :我试图在我的反应项目中实现深层链接(不确定这是否是正确的术语)我想要做的是:

  1. the admin gets a mail: "this user can't login: http://localhost:3000/admin/customer-profile/b1d4a11f-4f6c-4dc1-98a9-ac0c30486c16"管理员收到邮件:“此用户无法登录:http://localhost:3000/admin/customer-profile/b1d4a11f-4f6c-4dc1-98a9-ac0c30486c16”

  2. the admin is not logged in管理员未登录

  3. the admin clicks the link and is forwarded to the login page管理员单击链接并转发到登录页面

  4. the admin enters his login details管理员输入他的登录详细信息

(the new part) (新的部分)

  1. he is forwarded to /admin/customer-profile/b1d4a11f-4f6c-4dc1-98a9-ac0c30486c16他被转发到 /admin/customer-profile/b1d4a11f-4f6c-4dc1-98a9-ac0c30486c16

so in the fifth point the admin is redirected to the page link was for instead of going to the homepage.所以在第五点,管理员被重定向到页面链接,而不是去主页。

Below are some snippets to give more info regarding this:以下是一些片段,可提供有关此的更多信息:

    <div className="App">
      {ready && <Router basename="/admin">

        <Switch>
          <Route path="/login">
            <Login />
          </Route>
          <Route path="/privacypolicy">
            <PrivacyPolicy />
          </Route>
          <Route path="/loginhelp">
            <Help />
          </Route>
          <Route path="/termsconditions">
            <TermsConditions />
          </Route>
          <PrivateRoute path="/logout">
            <Logout />
          </PrivateRoute>

          <Route path="/">
            <SideBar />
            <Switch>
              <PrivateRoute path="/tier-list">
                <TierList />
              </PrivateRoute>
              <PrivateRoute path="/privacy-policy">
                <PrivacyPolicy />
              </PrivateRoute>
              <PrivateRoute path="/help">
                <Help />
              </PrivateRoute>
            </Switch>
          </Route>
        </Switch>
      </Router>}
    </div>

Below is how PrivateRoute looks like :以下是 PrivateRoute 的样子:

  function PrivateRoute({ children, ...rest }: any) {
    return (
      <Route
        {...rest}
        render={({ location }) =>
          userInfo != null ? (
            children
          ) : (
            <Redirect
              to={{
                pathname: "/login",
                state: { from: location }
              }}
            />
          )
        }
      />
    )
  }

Any ideas How I shall modify this part to achieve the functionality?任何想法我将如何修改这部分以实现功能?

Okay so I solved the problem.好的,所以我解决了这个问题。 What I did was that I passed a pathName prop to the Login component and then if the user was logged in I redirect him to that page but if not then rendered the login page.我所做的是将 pathName 属性传递给 Login 组件,然后如果用户已登录,我会将他重定向到该页面,但如果没有,则呈现登录页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM