为什么不使用React路由在React JS的父布局中呈现组件?

时间:2018-12-10 13:04:18

标签: reactjs react-router

我在前端和管理面板上有两种不同的布局。用于管理面板的组件在admin布局中呈现正确性,但对于前端,它无法正确切换路由,也无法在前端布局中呈现。 当我在index.js中不使用确切的属性时,它甚至也不适用于管理面板路由。 我也参考了这个链接 Nested Routes not rendering with React Router v4 但这对我不起作用。

import Dashboard from "layouts/Dashboard/Dashboard.jsx";
import Login from "components/FrontEnd/Login";
import Frontend from  "layouts/Frontend/Frontend.jsx";
import AdminLogin from  "layouts/Dashboard/AdminAuth.jsx";


var indexRoutes = [

    { path: "/", name: "Frontend", component: Frontend , exactPro:true},
    { path: "/login", name: "FrontendLogin", component: Login , exactPro:false},
    { path: "/Admin", name: "Home", component: Dashboard, exactPro:false },
    { path: "/Admin-login", name: "AdminLogin", component: AdminLogin, exactPro:false}

];

export default indexRoutes;

Index.js

import React from "react";
import ReactDOM from "react-dom";
import { Router, Route, Switch } from 'react-router-dom'
import indexRoutes from "routes/index.jsx";
import { history } from './helper/history';
import store from "./redux/store/index";
import { Provider } from "react-redux";

ReactDOM.render(
    <Provider store={store}>
        <Router history={history} >
            <Switch>
              {indexRoutes.map((prop, key) => {
                return <Route exact={prop.exactPro}  path={prop.path} component={prop.component} key={key} />;
              })}
            </Switch>
        </Router>
    </Provider>,
  document.getElementById("root")
);

.......

import Dashboard from "components/Admin/Dashboard";
import UserProfile from "views/UserProfile/UserProfile";

const dashboardRoutes = [
  {
    path: "/Admin/dashboard",
    name: "Dashboard",
    icon: "pe-7s-graph",
    component: Dashboard,
    showMenu:true,
    showMenuFront:false,
    iconImagePath:dashboardIcon,
    permission:'both'
  },
  {
    path: "/Admin/user",
    name: "User Profile",
    icon: "pe-7s-user",
    component: UserProfile,
    showMenu:false,
    showMenuFront:false,
    permission:'both'
  },
  { redirect: true, path: "/Admin", to: "/Admin/dashboard", name: "Dashboard" }
];

export default dashboardRoutes;

.........

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

export const AdminAuthRoute = ({ component: Component, ...rest }) => (
    <Route {...rest} render={(props, matchProps) => (
        localStorage.getItem('admin-user') 
        ? <Component {...props} {...matchProps} /> 
        : <Redirect to={{ pathname: '/Admin-login', state: { from: props.location } }} />
    )} />
)

..........

import React, { Component } from "react";
import {  Switch, Redirect } from "react-router-dom";
import dashboardRoutes from "routes/dashboard.jsx";
import {  AdminAuthRoute } from 'helper/PrivateRouteAdmin';

class DashboardPage extends Component {
  constructor(props) {
    super(props);
  }


  render() {

    return (
      <div className="wrapper">
        <div id="main-panel" className="main-panel" ref="mainPanel">
          <Switch>
            {
              dashboardRoutes.map((prop, key) => {
                console.log("prop redirect", prop.redirect);
                if (prop.redirect){
                  return <Redirect from={prop.path} to={prop.to} key={key} test="haha" />;
                }
                console.log('prop.path 111', prop.path);
                return (
                  <AdminAuthRoute   path={prop.path}  component={prop.component} key={key}  />
                );
              })
            }
          </Switch>
          <Footer />
        </div>
      </div>
    );
  }
}


export default Dashboard;

......

 import Home from "components/FrontEnd/Home";
    import HowItWorks from "components/FrontEnd/HowItWorks";
    import AboutUs from "components/FrontEnd/AboutUs";

    const FrontEndRoutes = [
      {
        path          : "/",
        name          : "Home",
        component     : Home,
        showMenu      : true,
        number        : 1
      },
      {
        path          : "/How_It_Works",
        name          : "How it works",
        component     : HowItWorks,
        showMenu      : true,
        number        : 2
      },
      {
        path          : "/About_Us",
        name          : "About Us",
        component     : AboutUs,
        showMenu      : true,
        number        : 3
      }
    ];

    export default FrontEndRoutes;

...........

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

export const FrontEndAuthRoute = ({ component: Component, ...rest }) => (
    <Route {...rest} render={(props, matchProps) => (
        localStorage.getItem('music-director') 
        ? <Component {...props} {...matchProps} /> 
        : <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
    )} />
)

......

import React, { Component } from "react";
import {  Switch, Redirect } from "react-router-dom";
import FrontEndRoutes from "routes/FrontEndRoutes.jsx";
import {  FrontEndAuthRoute } from 'helper/PrivateRouteFrontEnd';

class Frontend extends Component {
  constructor(props) {
    super(props);
  }
  render() {

    return (
      <div className="wrapper">
        <div id="main-panel" className="main-panel" ref="mainPanel">
          <Switch>
            {
              FrontEndRoutes.map((prop, key) => {
                if (prop.redirect){
                    return <Redirect from={prop.path} to={prop.to} key={key} test="haha" />;
                }
                return (
                    <FrontEndAuthRoute   path={prop.path}  component={prop.component} key={key}  />
                );

              })
            }
          </Switch>
        </div>
      </div>
    );
  }
}
export default Frontend;

1 个答案:

答案 0 :(得分:0)

在编写嵌套路由时,您需要注意很多事情

  1. 嵌套路由后,需要确保父路由没有确切的关键字。例如,具有path = '/'且具有子路由/home的路由将不会在/home上呈现Home组件,因为顶层本身不匹配。
  2. 您需要确保在Switch组件中编写Routes时,前缀路由路径位于开头。

考虑到以上几点,需要在您的应用中进行以下更改

import Dashboard from "layouts/Dashboard/Dashboard.jsx";
import Login from "components/FrontEnd/Login";
import Frontend from  "layouts/Frontend/Frontend.jsx";
import AdminLogin from  "layouts/Dashboard/AdminAuth.jsx";


var indexRoutes = [

    { path: "/Admin-login", name: "AdminLogin", component: AdminLogin, exactPro:false}
    { path: "/login", name: "FrontendLogin", component: Login , exactPro:false},
    { path: "/Admin", name: "Home", component: Dashboard, exactPro:false },
    { path: "/", name: "Frontend", component: Frontend , exactPro:false},

];

export default indexRoutes;

import Home from "components/FrontEnd/Home";
import HowItWorks from "components/FrontEnd/HowItWorks";
import AboutUs from "components/FrontEnd/AboutUs";

const FrontEndRoutes = [
  {
    path          : "/How_It_Works",
    name          : "How it works",
    component     : HowItWorks,
    showMenu      : true
  },
  {
    path          : "/About_Us",
    name          : "About Us",
    component     : AboutUs,
    showMenu      : true
  },
  {
    path          : "/",
    name          : "Home",
    component     : Home,
    showMenu      : true
  },
];

export default FrontEndRoutes;