嵌套路由的实现(react)

文章目录

  • 基本路由的使用
    • 模板文件index.html
    • 入口文件index.js
    • 入口组件App.js
    • pages/Home/index.jsx(Home组件)
    • pages/About/index.jsx(About组件)
  • 嵌套路由的实现
    • Home/index.jsx(Home组件)
    • News/index.jsx(News组件)
    • Message/index.jsx(Message组件)

基本路由的使用

在这里插入图片描述

本篇的vue项目,代码涉及的主要文件有:

  1. 模板文件index.html
  2. 入口文件index.js
  3. 入口组件App.js
  4. 路由组件Home:pages/Home/index.jsx
  5. 路由组件About:pages/About/index.jsx

模板文件index.html

DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="%PUBLIC_URL%/css/bootstrap.css">
    <title>React Apptitle>
  head>
  <body>
    <noscript>You need to enable JavaScript to run this app.noscript>
    <div id="root">div>
  body>
html>

入口文件index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from "react-router-dom";
import App from './App';

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById("root")
);

入口组件App.js

import React, { Component } from 'react'
import {NavLink,Route} from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";

export default class App extends Component {
  render() {
    return (
      <div>
        <div className="row">
          <div className="col-xs-offset-2 col-xs-8">
            <div className="page-header"><h2>Vue Router Demo</h2></div>
          </div>
        </div>
        <div className="row">
          <div className="col-xs-2 col-xs-offset-2">
            <div className="list-group">
              <NavLink className="list-group-item" activeClassName='active' to="/about">About</NavLink>
              <NavLink className="list-group-item" activeClassName='active' to="/home">Home</NavLink>
            </div>
          </div>
          <div className="col-xs-6">
            <div className="panel">
              <div className="panel-body">
                <Route path="/about" component={About}></Route>
                <Route path="/home" component={Home}></Route>
              </div>
            </div>
          </div>
        </div>
      </div>
    )
  }
}

pages/Home/index.jsx(Home组件)

import React, { Component } from 'react'

export default class Home extends Component {
  render() {
    return (
        <h2>我是Home的内容</h2>
    )
  }
}

pages/About/index.jsx(About组件)

import React, { Component } from 'react'

export default class About extends Component {
  render() {
    return (
        <h2>我是About的内容</h2>
    )
  }
}

嵌套路由的实现

嵌套路由的实现(react)_第1张图片
基于以上实例,来实现嵌套路由。路由结构如下:

  • /about,About组件
  • /home,Home组件
    • /home/news,News组件
    • /home/message,Message组件

在Home文件夹下新建文件夹:News,News下新建文件:index.jsx,即News组件;
在Home文件夹下新建文件夹:Message,Message下新建文件:index.jsx,即Message组件。

代码更新涉及的文件有:

  1. pages/Home/index.jsx,即Home组件
  2. pages/Home/News/index.jsx,即News组件
  3. pages/Home/Message/index.jsx,即Message组件

Home/index.jsx(Home组件)

import React, { Component } from 'react'
import { NavLink,Route } from 'react-router-dom'
import News from "./News";
import Message from "./Message"

export default class Home extends Component {
  render() {
    return (
      <div>
        <h2>Home组件内容</h2>
        <div>
          <ul className="nav nav-tabs">
            <li>
              <NavLink to="/home/news" className="list-group-item" activeClassName="active">News</NavLink>
            </li>
            <li>
              <NavLink to="/home/message" className="list-group-item" activeClassName="active">Message</NavLink>
            </li>
          </ul>
          <Route path="/home/news" component={News}></Route>
          <Route path="/home/message" component={Message}></Route>
        </div>
      </div>
    )
  }
}

News/index.jsx(News组件)

import React, { Component } from 'react'

export default class News extends Component {
    render() {
        return (
            <ul>
                <li>news001</li>
                <li>news002</li>
                <li>news003</li>
            </ul>
        )
    }
}

Message/index.jsx(Message组件)

import React, { Component } from 'react'

export default class index extends Component {
    render() {
        return (
            <div>
                <ul>
                    <li>
                        <a href="/message1">message001</a>&nbsp;&nbsp;
                    </li>
                    <li>
                        <a href="/message2">message002</a>&nbsp;&nbsp;
                    </li>
                    <li>
                        <a href="/message/3">message003</a>&nbsp;&nbsp;
                    </li>
                </ul>
            </div>
        )
    }
}

你可能感兴趣的:(React17/18,嵌套路由,react)