react项目实战教程(react项目实战)

文章目录项目实战前的准备工作项目实战一.搭建项目的基本页面及外层路由1-1配置基本页面1-2配置路由1-3需要最外层去渲染路由视图1-4需要配置内层App路由1-5路由的懒加载二.利用Antd实现组件2-1使用Antd的Layout进行布局2-2可以设计个Logo2-3实现左侧的菜单展示2-4实现左侧的小图标2-5实现点击菜单切换总结需要下载的组件项目实战前的准备工作React基础…

大家好,又见面了,我是你们的朋友全栈君。

项目实战前的准备工作

React基础
React组件
React-Router——Reac路由的学习
React高阶组件及CRA的定制
React中使用Antd组件

React项目实战(一)

React项目实战(二)

搭建项目的基本页面及外层路由

1-1配置基本页面

在项目根目录src文件下创建views文件夹
在这里插入图片描述
然后在views文件夹里创建所需要页面,
在相应文件夹下创建index.js文件,这样引入所需页面的时候直接写./login即可,不需要再写./login/index.js

  • article(index.js / Edit.js) 文章列表 文章编辑
  • dashboard(index.js) 仪表盘
  • login(index.js) 登录
  • settings(index.js) 设置
  • notfound(index.js) 404

在这里插入图片描述

  • 最后再创建views/index.js,并在文件里导出页面
import Article from "./article"      //文章列表
import ArticleEdit from "./article/Edit"//文章编辑
import Dashboard from "./dashboard"//仪表盘
import Login from "./login"//登录
import Notfound from "./notfound"//404
import Settings from "./settings"//设置

export { 
   
    Article,
    ArticleEdit,
    Dashboard,
    Login,
    Notfound,
    Settings
}

1-2配置路由

  • 安装路由:yarn add react-router-dom或者npm i react-router-dom
  • 在src文件下新建routes文件夹,在其下新建index.js文件并配置路由
    在这里插入图片描述
    routes/index.js
import  { 
   
    Article,
    ArticleEdit,
    Dashboard,
    Login,
    Notfound,
    Settings
} from "../views"   //引入页面


// /login 页面 /404 页面 
export const mainRoute = [
    { 
   
        pathname:"/login",
        component:Login
    },
    { 
   
        pathname:"/404",
        component:Notfound
    }
]


// /admin/XXX dashboard article articleEdit settings 管理页面
export const admainRoute = [
    { 
   
        pathname:"/admin/dashboard",
        component:Dashboard
    },
    { 
   
        pathname:"/admin/article",
        component:Article,
        exact:true  //配置App内置路由需要exact属性,详情见下文
    },
    { 
   
        pathname:"/admin/article/edit/:id",
        component:ArticleEdit
    },
    { 
   
        pathname:"/admin/settings",
        component:Settings
    },
]

1-3需要最外层去渲染路由视图

  • 在src/index.js文件里渲染路由视图
    在这里插入图片描述
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { 
   HashRouter as Router,Route,Redirect,Switch} from "react-router-dom"
import { 
   mainRoute} from "./routes"
ReactDOM.render(
  <Router>
    <Switch>
      <Route path="/admin" component={ 
   App}/> //admin主页面的路由 进入到App组件
      { 
   
        mainRoute.map(route=>{ 
   
          return <Route path={ 
   route.pathname} component={ 
   route.component}/>
        })
      }  //遍历login 404 的路由
      <Redirect to="/admin" from="/" exact/>  //重定向 首页为admin
      <Redirect to="/404" />		//如果路径与之前都不匹配,则返回404页面
    </Switch>
  </Router>
  ,
  document.getElementById('root')
);

1-4需要配置内层App路由

  • 在src/App.js文件里渲染路由视图
    在这里插入图片描述
import React, { 
    Component } from 'react'
import { 
   admainRoute} from "./routes"
import { 
   Route,Redirect,Switch} from "react-router-dom"
export default class App extends Component { 
   
  render() { 
   
    return (
      <Switch>
        { 
   
          admainRoute.map(route=>{ 
   
            return <Route path={ 
   route.pathname} component={ 
   route.component} exact={ 
   route.exact}/>
          })
        }   //admin页面里的路由 dashboard article articleEdit settings
        <Redirect to={ 
   admainRoute[0].pathname} from="/admin" exact/>  //重定向到 admin页面
        <Redirect to="/404" />
      </Switch> 
    )
  }
}
  • 后续发现了问题 /admin/article显示Article中的内容 但是/admin/article/edit/2的时候不显示ArticleEdit中的内容
    解决方法,直接在routes/index.js里面添加一个标志exact然后遍历路由的时候判断是否要添加exact属性
    { 
   
        pathname:"/admin/article",
        component:Article,
        exact:true
    },

    { 
   
              admainRoute.map(route=>{ 
   
                return <Route    exact={ 
   route.exact}/>
              })
    }

1-5 路由的懒加载

  • 下载react-loadable
    通过yarn add react-loadable安装
    在npm官网中搜索查阅使用方法
    在这里插入图片描述

  • 并新建src/component/loading/index.js文件,当懒加载未完成时,会显示该页面的内容
    在这里插入图片描述

  • 更改src/views/index.js文件

import Loadable from 'react-loadable';
import Loading from '../components/loading';
//需要将对外的普通组件需要进行懒加载
const Article = Loadable({ 
   
    loader: () => import('./article'),
    loading: Loading,
});
const Dashboard = Loadable({ 
   
    loader: () => import('./dashboard'),
    loading: Loading,
});
const ArticleEdit = Loadable({ 
   
    loader: () => import('./article/Edit'),
    loading: Loading,
});
const Login = Loadable({ 
   
    loader: () => import('./login'),
    loading: Loading,
});
const Notfound = Loadable({ 
   
    loader: () => import('./notfound'),
    loading: Loading,
});
const Settings = Loadable({ 
   
    loader: () => import('./settings'),
    loading: Loading,
});


export  { 
   
    Article,
    ArticleEdit,
    Dashboard,
    Login,
    Notfound,
    Settings
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128605.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2020=1024+996,程序员本命年,去外包公司公司要小心了。

    2020=1024+996,程序员本命年,去外包公司公司要小心了。2020第一天,我从外包公司离职了,过来人的经验之谈,外包公司本身就是赚差价的,可以去,但是需要谨慎》》》

    2022年7月2日
  • Folium_个人基础信息介绍

    Folium_个人基础信息介绍folium基础内容介绍1.简介​ folium是js上著名的地理信息可视化库leafet.js为Python提供的接口,通过它,我们可以通过在Python端编写代码操纵数据,来调用leaflet的相关功能,基于内建的osm或自行获取的osm资源和地图原件进行地理信息内容的可视化,以及制作优美的可交互地图,是通过不断添加图层元素来定义一个Map对象,最后以几种方式将Map对象展现出来。​ 而在Map对象的生成形式上,可以在定义所有的图层内容之后,将其保存为html文件在浏览器中独立显示,也可以基于j

    2022年10月29日
  • 心灵鸡汤【5】

    心灵鸡汤【5】

    2021年8月20日
  • Object转String的几种方法

    Object转String的几种方法1.object.toString()方法   这种方法要注意的是object不能为null,否则会报NullPointException,一般别用这种方法。2.String.valueOf(object)方法   这种方法不必担心object为null的问题,若为null,会将其转换为"null"字符串,而不是null。这一点要特别注意。"null"和null不是一个概念。3.(…

    2022年6月11日
  • let暂时性死区理解「建议收藏」

    let暂时性死区理解「建议收藏」Let中提到的暂时性死区到底是什么,它的产生原因是? ES6规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。总之,在代码块内,使用let命令声明变量之前,该变量都是不可用的。这在语法上,称为“暂时性死区”,也就是说使用let声明的变量都是先声明再使用,不存在变量提升问题。letmyname=’sxx’;{console.log(myname);l…

    2022年6月24日
  • SpringBoot Test及注解详解

    SpringBoot Test及注解详解一、SpringBootTest介绍SpringTest与JUnit等其他测试框架结合起来,提供了便捷高效的测试手段。而SpringBootTest是在SpringTest之上的再次封装,增加了切片测试,增强了mock能力。整体上,SpringBootTest支持的测试种类,大致可以分为如下三类:单元测试:一般面向方法,编写一般业务代码时,测试成本较大。涉及到的注解有@Test。 切片测试:一般面向难于测试的边界功能,介于单元测试和功能测试之间。涉及到的注解有@RunWith

    2022年5月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号