zoukankan      html  css  js  c++  java
  • react嵌套路由,并设置默认子路由

    弄了好长时间,记得之前写的时候没问题呀,现在不知道哪里出现问题,后来才发现 是 exact  和 跳转子路由 路径的问题,哎

    App.js

    import React, {lazy, Suspense}  from "react";
    import { Switch, Route } from 'react-router-dom';
    function App() {
      return (
        <div className="App">
          <Suspense fallback={React.Fragment}>
            <Switch><Route path="/" exact component={lazy(() => import('./views/Login'))}>Route>
          
          // 切记!!!!!! 这里不能加 exact 否则子路由匹配不进去 <Route path="/MyService" component={lazy(() => import('./views/MyService'))}>Route>
    <Route path="*" component={lazy(() => import('./views/Error/Error'))}>Route> Switch> Suspense> div> ); } export default App;
    MyService/index.js
    import React from 'react';
    import { Switch, Route, useHistory } from 'react-router-dom';
    import { Button } from 'antd-mobile';
    // import routes from 'src/router';
    import dent from './dent/dent'
    import high from './high/high'
    
    function MyService () {
      const history = useHistory()
      return (
        <div>
          MyService
        // !!!!!! 这里切记写上完整的路由路径,,即 父路由 + 子路由
    <Button type="primary" onClick={() => history.push('/MyService/dent')}>AButton> <Button type="primary" onClick={() => history.push('/MyService/high')}>BButton> <Switch>
        
         // !!!!!! 这里切记写上完整的路由路径,,即 父路由 + 子路由
          <Route path="/MyService/dent" component={dent}>Route> 
          <Route path="/MyService/high" component={high}>Route>
        Switch>
       div>
      )
    }

    export default MyService;

    MyService/dent/dent.jsx

    import React from 'react';
    
    function dent () {
      return (
        <div>
          dent
        div>
      )
    }
    
    export default dent;

    MyService/high/high.jsx

    import React from 'react';
    
    function high () {
      return (
        <div>
          high
        div>
      )
    }
    
    export default high;

    默写子路由 ,修改 MyService/index.js

    import React from 'react';
    import { Switch, Route, useHistory } from 'react-router-dom';
    import { Button } from 'antd-mobile';
    // import routes from 'src/router';
    import dent from './dent/dent'
    import high from './high/high'
    
    function MyService () {
      const history = useHistory()
      return (
        <div>
          MyService
          <Button type="primary" onClick={() => history.push('/MyService/')}>AButton>
          <Button type="primary" onClick={() => history.push('/MyService/high')}>BButton>
          <Switch>
            <Route exact path="/MyService/" component={dent}>Route>
            <Route path="/MyService/high" component={high}>Route>
          Switch>
        div>
      )
    }
    
    export default MyService;

    参考

    https://www.cnblogs.com/yuyujuan/p/10140264.html

  • 相关阅读:
    防止SQL注入
    Sql Server参数化查询之where in和like实现详解
    NET下载文件报错System.UnauthorizedAccessException的解决方法
    hibernate 中的session和事务(Transaction)
    ASP.net MVC 文件下载的几种方法
    SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
    NHibernate 中删除数据的几种方法
    SQL、LINQ、Lambda 三种用法(转)
    .net MVC 单页面 多个(行)数据修改
    绘制你的第一个图表(jquery-flot-line-chart)
  • 原文地址:https://www.cnblogs.com/-roc/p/15066666.html
Copyright © 2011-2022 走看看