4

我的应用程序目前分为 3 个部分:

  • 前端
  • 行政
  • 错误

Frontend、Administration 和 Error 组件都有自己的样式。

Frontend 和 Administration 组件也有自己的 Switch 组件来导航它们。

我面临的问题是我无法在没有 Redirect 组件的情况下访问 NoMatch 路径。但是当我这样做时,我会丢失浏览器 URL 中的错误路径。

当内部 Switch 组件没有匹配的路由时,是否有机会在其父 Switch 组件中不断搜索?

然后我就可以点击 NoMatch 路由并在 URL 中保留错误的路径。

编辑:我用按预期工作的最终解决方案更新了下面的答案。

const Frontend = (props) => {
  const { match } = props;
  return (<div>
    <h1>Frontend</h1>
    <p><Link to={match.path}>Home</Link></p>
    <p><Link to={`${match.path}users`}>Users</Link></p>
    <p><Link to="/admin">Admin</Link></p>
    <p><Link to={`${match.path}not-found-page`}>404</Link></p>
    <hr />
    <Switch>
      <Route exact path={match.path} component={Home} />
      <Route path={`${match.path}users`} component={Users} />
      {
      // Workaround
      }
      <Redirect to="/error" />
    </Switch>
  </div>);
};

const Admin = (props) => {
  const { match } = props;
  return (<div>
    <h1>Admin</h1>
    <p><Link to={match.path}>Dashboard</Link></p>
    <p><Link to={`${match.path}/users`}>Edit Users</Link></p>
    <p><Link to="/">Frontend</Link></p>
    <p><Link to={`${match.path}/not-found-page`}>404</Link></p>
    <hr />
    <Switch>
      <Route exact path={match.path} component={Home} />
      <Route path={`${match.path}/users`} component={Users} />
      {
      // Workaround
      }
      <Redirect to="/error" />
    </Switch>
  </div>);
};

const ErrorPage = () =>
  <div>
    <h1>404 not found</h1>
    <p><Link to="/">Home</Link></p>
  </div>;

const App = () => (
  <div>
    <AddressBar />
    <Switch>
      <Route path="/error" component={ErrorPage} />
      <Route path="/admin" component={Admin} />
      <Route path="/" component={Frontend} />
      {
      // this should render the error page
      // instead of redirecting to /error
      }
      <Route component={ErrorPage} />
    </Switch>
  </div>
);
4

2 回答 2

5

这是此类需求的最终解决方案。为了使它工作,我们使用该位置的状态属性。在内部路由的重定向中,我们将状态设置为error: true. 在 GlobalErrorSwitch 上,我们检查状态并渲染错误组件。

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

const Home = () => <div><h1>Home</h1></div>;
const User = () => <div><h1>User</h1></div>;
const Error = () => <div><h1>Error</h1></div>

const Frontend = props => {
  console.log('Frontend');
  return (
    <div>
      <h2>Frontend</h2>
      <p><Link to="/">Root</Link></p>
      <p><Link to="/user">User</Link></p>
      <p><Link to="/admin">Backend</Link></p>
      <p><Link to="/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/' component={Home}/>
        <Route path='/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

const Backend = props => {
  console.log('Backend');
  return (
    <div>
      <h2>Backend</h2>
      <p><Link to="/admin">Root</Link></p>
      <p><Link to="/admin/user">User</Link></p>
      <p><Link to="/">Frontend</Link></p>
      <p><Link to="/admin/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/admin' component={Home}/>
        <Route path='/admin/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

class GlobalErrorSwitch extends Component {
  previousLocation = this.props.location

  componentWillUpdate(nextProps) {
    const { location } = this.props;

    if (nextProps.history.action !== 'POP'
      && (!location.state || !location.state.error)) {
        this.previousLocation = this.props.location
    };
  }

  render() {
    const { location } = this.props;
    const isError = !!(
      location.state &&
      location.state.error &&
      this.previousLocation !== location // not initial render
    )

    return (
      <div>
        {          
          isError
          ? <Route component={Error} />
          : <Switch location={isError ? this.previousLocation : location}>
              <Route path="/admin" component={Backend} />
              <Route path="/" component={Frontend} />
            </Switch>}
      </div>
    )
  }
}

class App extends Component {
  render() {
    return <Route component={GlobalErrorSwitch} />
  }
}

export default App;
于 2017-03-06T20:58:13.207 回答
0

所有子组件路由都包装在<Switch>父组件(组件switch内部app)中,您实际上并不是子组件中的开关。

只需删除子 switch.component 并让<App <Switch>捕获中的 404 丢失。

于 2017-03-05T11:52:12.650 回答