简体   繁体   English

路由不匹配时,未显示未找到页面

[英]not found page is not shown when route is not matched

As per me, I have a bit complex routing because I have to handle different domain for different modules. 按照我的观点,我的路由有些复杂,因为我必须为不同的模块处理不同的域。 That is why i configured the routes in a following way. 这就是为什么我以以下方式配置路由。

Here it is 这里是

ReactDOM.render(
    <Provider store={store}>
      <ConnectedRouter history={history}>
        <Root />
      </ConnectedRouter>
    </Provider>,
  MOUNT_NODE,
);

class App extends React.Component {
  render() {
    return (
      <Switch>
        <UnauthenticatedRoute path="/auth" component={AsyncLogin} {...this.props} />
        <AuthenticatedRoute path="/" component={AsyncHome} {...this.props} />
      </Switch>
    );
  }
}

class Home extends React.Component<propsCheck> {
  componentDidMount() {
    this.props.getUser();
  }

  renderRoutes(userRole, roles, userData, props) {
    const domain = window.location.hostname; // domain will be like app.abc.com, app.def.com.
    switch (domain) {
      case GROWTH_URL:
        return growthRoutes(userRole, roles, userData, props);
      case CONTENT_URL:
        return contentRoutes(userRole, roles, userData, props);
      default:
        return growthRoutes(userRole, roles, userData, props);
    }
  }

  render() {
    if (this.props.loading) {
      return <Spinner background="none" />;
    }
    return <Switch>{this.renderRoutes(userRole, roles, userData, this.props)}</Switch>;
  }
}

const withConnect = connect(
  mapStateToProps,
  mapDispatchToProps,
)(Home);
export default withRouter(withConnect);


function NotFoundPage() {
  return <div>Not found</div>;
}

export function growthRoutes(userRole, roles, userData, props) {
  return (
    <Switch>
      <Route
        exact
        path="/"
        render={() =>
          (!isEmpty(userRole) && userRole.client !== null && isClient(roles)) ||
          (!isEmpty(userData) && userData.client !== null && isClient(userData.roles)) ? (
            <Redirect to={`${!isEmpty(userRole) ? userRole.client[0].company_slug : userData.company[0]}`} />
          ) : (
            <Redirect to="/clients" />
          )
        }
      />
      <DashboardRoute path="/clients" component={Clients} {...props} />
      <DashboardRoute path="/:company/" component={ClientDetail} {...props} />
      <DashboardRoute path="/:company/client_detail" component={ClientDetail} {...props} />
      <DashboardRoute path="/:company/edit-client" component={Admin(Client)} {...props} />
      <DashboardRoute path="/tasks" component={Tasks} {...props} />
      <DashboardRoute to="*" component={NotFoundPage} />
    </Switch>
  );
}

I could not show NotFoundPage this way and could not figure out why it is not working. 我无法以这种方式显示NotFoundPage,也无法弄清为什么它不起作用。 I have no idea where should i use the snippet <Route path="*" component={NotFoundPage}> . 我不知道应该在哪里使用代码段<Route path="*" component={NotFoundPage}> Nowhere it works. 无处工作。 Can anyone look at this, please? 有人可以看一下吗?

The problem is with respect to how you define your routes. 问题在于您如何定义路线。 since you have a Route /:company/ , it will match anything which starts with / like /abc/ , /abc/sfsf/fsgs and so on 由于您拥有Route /:company/ ,因此它将匹配以/开头的任何内容,例如/abc//abc/sfsf/fsgs等。

So what you need to do is to first reorder your Routes, so that all Routes within Switch component work like 因此,您需要做的是首先对路由进行重新排序,以便Switch组件中的所有路由都可以像

<Switch>
     <Route
        exact
        path="/"
        render={() =>
          (!isEmpty(userRole) && userRole.client !== null && isClient(roles)) ||
          (!isEmpty(userData) && userData.client !== null && isClient(userData.roles)) ? (
            <Redirect to={`${!isEmpty(userRole) ? userRole.client[0].company_slug : userData.company[0]}`} />
          ) : (
            <Redirect to="/clients" />
          )
        }
      />
      <DashboardRoute path="/clients" component={Clients} {...props} />
      <DashboardRoute path="/tasks" component={Tasks} {...props} />
      <DashboardRoute path="/:company/client_detail" component={ClientDetail} {...props} />
      <DashboardRoute path="/:company/edit-client" component={Admin(Client)} {...props} />
      <DashboardRoute exact path="/:company" component={ClientDetail} {...props} />
      <DashboardRoute to="*" component={NotFoundPage} />
</Switch>

Now in the above case routes like /abc/ffg/ will show NotFoundPage but still Routes like /abc will still match /:company where company will be abc . 现在在上述情况下, /abc/ffg/路由将显示NotFoundPage,但是/abc类的路由仍会匹配/:company ,而company将是abc So what you need to do in ClientDetail, check if the company is a valid company and return the Correct view and if it isn't you return the NotFoundPage 因此,您需要在ClientDetail中执行的操作,检查公司是否为有效公司,然后返回正确视图,如果不是,则返回NotFoundPage

ClientDetails 客户详情

render() {
    if(!validCompanies.includes(this.props.match.company)) {
        return <DashboardRoute to="*" component={NotFoundPage} />
    }
    // Normal component logic here
}

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

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