react router官方文档_Vue 转React开发过程 (快速上手)

简单看了下React文档 凭借开发vue几年经验的自信就去仿vue的一个后台系统使用react开发

我喜欢这种边做边学的学习方式

小白一个 记录成长过程 大佬勿喷

第一步 搭建环境

react文档中会有详细的教程 这里我就不多说了 链接给你

入门教程: 认识 React – React​react.docschina.org
a6eff96e875b94b9889af7c9a553f859.png

第二步 路由(简单版--下面放嵌套版)

APP.js

注意 Router 标签只能有一个 并且要包住所有 Switch Link Route

import React from "react";
import {BrowserRouter as Router, Route, Redirect, Switch, Link} from 'react-router-dom';
import LayoutCom from './Layout/index'

import "./App.css";
import Login from "./login/index";
import Home form "./home/index"
import NotFund from "./notFund/index"

const routes = [
  { path: '/', component: Home, title: '首页' },
  { path: '/login', component: Login },
  { path: '*', component: NotFund }
]

class App extends React.Component {
  render() {
    return (
      <Router>
        <div>
          <div className="nav">
            <ul>
              {routes.map(item => {
                if (item.path === '*' || item.path === '/login') return null
                return (
                <li><Link to={item.path}/>{item.title}</li>
                )
              })}
            </ul>
          </div>
          <div className="content">
            {/* 这里的作用可以理解为vue中的router-view */}
            <Switch>
              {routes.map(item => {
                return (
                  <Route path={item.path} exact component={item.component}/>
                )
              })}
            </Switch>
          </div>
        </div>
      </Router>
    );
  }
}

export default App;

嵌套路由 等同于vue中的:

const routes = [
  { path: '/', redirect: '/app' },
  {
    path: '/app',
    component: Layout,
    children: [
      { path: '/app/details', component: Details }
    ]
  },
  { path: '*', component: NotFund }
]

对应React中写法 APP.js

class App extends React.Component {
  render() {
    return (
      <Router>
        <Switch>
          <Route exact path='/login' component={Login}></Route>
          <Route path='/app' component={LayoutCom}></Route>
          <Route exact path='/' component={() => <Redirect to='/app'></Redirect>}></Route>
          <Route exact path="*" component={NotFund}/>
        </Switch>
      </Router>
    );
  }
}

LayoutCom.js

import React from 'react'
import {Route, Link, Switch} from 'react-router-dom';
import { Layout, Menu, Breadcrumb } from "antd";
import {
  DesktopOutlined,
  PieChartOutlined
} from "@ant-design/icons";

import Home from '../home/index';
import Detail from '../detail/index';
import NotFund from '../notFund/index';

const { Header, Content, Footer, Sider } = Layout;
class LayoutCom extends React.Component {
  state = {
    collapsed: false,
  };

  onCollapse = (collapsed) => {
    console.log(collapsed);
    this.setState({ collapsed });
  };
  render () {
    const { collapsed } = this.state;
    return (
      <Layout style={{ minHeight: "100vh" }}>
        <Sider collapsible collapsed={collapsed} onCollapse={this.onCollapse}>
          <div className="logo" >崔二莲</div>
          <Menu theme="dark" defaultSelectedKeys={["1"]} mode="inline">
            <Menu.Item key="1" icon={<PieChartOutlined />}>
              <Link to="/app">首页</Link>
            </Menu.Item>
            <Menu.Item key="2" icon={<DesktopOutlined />}>
              <Link to="/app/detail">详情</Link>
            </Menu.Item>
          </Menu>
        </Sider>
        <Layout className="site-layout">
          <Header className="site-layout-background" style={{ padding: 0 }} />
          <Content style={{ margin: "0 16px" }}>
            <Breadcrumb style={{ margin: "16px 0" }}>
              <Breadcrumb.Item>User</Breadcrumb.Item>
              <Breadcrumb.Item>Bill</Breadcrumb.Item>
            </Breadcrumb>
            <Switch>
              <Route exact path="/app" component={Home}/>
              <Route exact path="/app/detail" component={Detail}/>
              <Route exact path="/*" component={NotFund}/>
            </Switch>
          </Content>
          <Footer style={{ textAlign: "center" }}>
            Ant Design ©2018 Created by Ant UED
          </Footer>
        </Layout>
      </Layout>
    )
  }
}
export default LayoutCom

后续继续更新 一起学习 一起成长

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值