0

react我使用, react-router,创建了一个应用程序react-reduxconnected-react-router并且具有以下结构:

- app
  - admin
    - category
      - categoryList
      - categoryAdd

我的想法是执行以下应用程序流程:

/admin -> /admin/category -> /admin/category/add

但由于某种原因,我看不到CategoryAdd渲染的最后一个组件()。

代码(沙盒链接在最后):

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { Route, Switch, Link } from "react-router-dom"; 
import { ConnectedRouter } from "connected-react-router";
import configureStore, { history } from "./configureStore";

import AdminPage from "./AdminPage";
import Category from "./Category";

const store = configureStore(/* provide initial state if any */);

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <>
        <Link to="/admin/category">Category</Link>
        <Switch>
          <Route exact path="/admin" component={AdminPage} />
          <Route exact path="/admin/category" component={Category} />
        </Switch>
      </>
    </ConnectedRouter>
  </Provider>,
  document.getElementById("root")
);

管理页面.js

import React from "react";
import { Link } from "react-router-dom";

export default function AdminPage() {
  return (
    <div style={{ border: "1px solid black", margin: 20 }}>
      <div>AdminPage</div>
      <Link to="/admin/category">Catgory page link</Link>
    </div>
  );
}

类别.js

import React from "react";
import { Route, Switch } from "react-router-dom";
import { withRouter } from "react-router-dom";
import CategoryList from "./CategoryList";
import CategoryAdd from "./CategoryAdd";

function Category() {
  return (
    <Switch>
      <Route path={`/admin/category`} render={() => <CategoryList />} />
      <Route path={`/admin/category/add`} render={() => <CategoryAdd />} />
    </Switch>
  );
}

export default withRouter(Category);

类别列表.js

import React from "react";
import { Link } from "react-router-dom";

export default function CategoryList() {
  return (
    <div style={{ border: "1px solid black", margin: 20 }}>
      CategoryTable <Link to="/admin/category/add">Add</Link>
    </div>
  );
}

CategoryAdd.js

export default function Add() {
  return <div style={{ border: "1px solid black", margin: 20 }}>Add Page</div>;
}

这是沙箱:https ://codesandbox.io/embed/dark-star-3fhs0

4

2 回答 2

1

您 using Switchwhich 仅匹配一条路线,因此您的第二条路线index.jsCategory组件将永远不会被渲染。

1)删除Switch组件和exact路由道具index.js

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <>
        <Link to="/admin/category">Category</Link>
        <Route path="/admin" component={AdminPage} />
        <Route path="/admin/category" component={Category} />
      </>
    </ConnectedRouter>
  </Provider>,
  document.getElementById("root")
);

Switch2 )删除Category

function Category() {
  return (
    <>
      <Route path={`/admin/category`} render={() => <CategoryList />} />
      <Route path={`/admin/category/add`} render={() => <CategoryAdd />} />
    </>
  );
}

这在您的代码沙箱中有效,但您的 CategoryAdd 组件已损坏,因为您没有 import React。所以将它添加到第一行:

import React from 'react';

结果 : 在此处输入图像描述

于 2019-06-23T12:33:03.547 回答
1

那是因为您路线exact上的关键字:/admin/category

<Route exact path="/admin/category" component={Category} />

这仅匹配/admin/category,但不匹配/admin/category/add

不幸的是,删除exact遗嘱只能解决您的第一个问题。

另一个是您使用Switch. 您应该删除Switch中的Index.js,因为一旦您匹配了您的/admin/category路线,它就不会匹配您的类别组件中的任何路线。

于 2019-06-23T12:42:11.410 回答