React Router v6使用路由嵌套和重定向

前言:v6版本与v5版本差异较大,以下为V6使用路由嵌套及路由重定向方法,子路由路径不用加"/",v6中会自动拼接

App.jsx

import React, { Component } from 'react'
import { Route,Routes,Navigate} from 'react-router-dom'
import About from './pages/About'
import Home from './pages/Home'
import Header from './components/Header'
import MyNavLink from './components/MyNavLink'
export default class App extends Component {
    

    render() {
        
        return (
            
            
{/* 链接路由 */} About Home
{/* 注册路由 */} {/* 首次进入页面是重定向到/about路径 */} }> }> }>
) } }

Home/index.jsx

import React, { Component } from 'react'
import MyNavLink from '../../components/MyNavLink'
import {Route,Routes,Navigate} from 'react-router-dom'
import News from './News'
import Messages from './Messages'
export default class Home extends Component {
    render() {
        return (
           

Home内容

News Messages
}> }> }>
) } }

Home/Messages/index.jsx

import React, { Component } from 'react'

export default class Messages extends Component {
    render() {
        return (
            
  • message1
  • message2
  • message3
  • message4
) } }

MyNavLink/index.jsx

import React, { Component } from 'react'
import { NavLink } from 'react-router-dom'
export default class MyVavLink extends Component {
    render() {
        return (
            
        )
    }
}

你可能感兴趣的:(前端,html,vue.js,前端,javascript)