目录
一、前端路由(改变URL,页面不进行整体刷新的两种方式)
1、URL 的hash
2、HTML5 的History
二、react-router
1、router 的两种方式
2、路由的示例
a、默认展示登录按钮,点击登录按钮后跳转到home 页面
4、NotFound 页面
5、路由的嵌套
6、编程式路由跳转
a、函数组件的路由跳转
b、类组件的路由跳转
7、路由传参
a、动态路由传参
b、拼接查询字符串
8、配置路由信息
前端路由的核心:改变URL时,页面不进行整体的刷新
history 接口是HTML5 新增的,它有六种模式改变URL 而不刷新页面
npm install react-router-dom
默认是pushState 操作
import { StrictMode } from "react"
import ReactDOM from "react-dom/client"
import { HashRouter } from "react-router-dom"
import App from "./App"
const root = ReactDOM.createRoot(document.querySelector("#root"))
root.render(
)
imort { Link, NavLink, Route, Routes } from 'react-router-dom'
home
about
} />
} />
import { Navigate } from 'react-router-dom'
export class App extends PureComponent {
constructor(props) {
super(props)
this.state = {
isLogin: false
}
}
login() {
this.setState({ isLogin: true })
}
render() {
const { isLogin } = this.state
return (
{ !isLogin ? : }
)
}
}
} />
} />
} />
} />
} />
} />
} />
} />
} >
} />
} />
} />
} />
} />
推荐
推荐
// 占位的组件
import { useNavigate } from "react-router-dom"
export function App(props) {
const navigate = useNavigate()
function navigateTo(path) {
navigate(path)
}
return {
}
}
export default App
export class Home extends PureComponent {
navigateTo(path) {
const { navigate } = this.props.router
navigate(path)
}
render() {
return (
推荐
排行榜
)
}
}
// 高阶函数:函数
function withRouter(WrapperComponent) {
return function(props) {
const navigate = useNavigate()
const router = { navigate }
return
}
}
export default withRouter(Home)
// with_router.js
import { useNavigate, usePrams } from "react-router-dom"
function withRouter(WrapperComponent) {
return function(props) {
const navigate = useNavigate()
const params = useParams()
const router = { navigate, params }
return
}
}
export default withRouter
} />
import { widthRouter } from "../hoc"
export class HomeSongMenu extends PureComponent {
constructor(props) {
super(props)
this.state = {
songMenus: [
{ id: 1, name: a },
{ id: 2, name: b },
{ id: 3, name: c },
]
}
}
NavigateToDetail(id) {
const { navigate } = this.props.router
navigate("/detail/" + id)
}
render() {
const { songMenus} = this.state
return (
{
songMenus.map(item => {
return - this.NavigateToDetail(item.id)}>{item.name}
})
}
)
}
}
export default withRouter(HomeSongMenu)
import { widthRouter } from '../hoc'
export class Detail extends PureComponent {
render() {
const { router } = this.props
const { params } = router
return (
id: { params.id }
)
}
}
export default widthRouter(Detail)
用户
} />
// with_router.js
import { useNavigate, usePrams, useSearchParams } from "react-router-dom"
function withRouter(WrapperComponent) {
return function(props) {
const navigate = useNavigate()
const params = useParams()
const [ searchParams, setSearchParams ] = useSearchParams()
const query = Object.fromEntries(searchParams)
const router = { navigate, params, query }
return
}
}
export default withRouter
export class User extends PureComponent {
render() {
const { router } = this.props
const { query } = router
return {
{ query.name } - { query.age }
}
}
}
export default withRouter(User)
// router/index.js
import { Navigate } from "react-router-dom"
const routes = [
{
path: "/",
element: ,
},
{
path: "/",
element: ,
children: {
{ path: "/home", element: },
{ path: "/home/recommend", element: },
{ path: "/home/ranking", element: },
}
},
{
path: "/detail/:id",
element: ,
},
{
path: "*",
element: ,
}
]
export default routes
// App.jsx
import routes from @/router/index.js
{ useRoutes(routes) }
9、路由懒加载
// router/index.js
import React from 'react'
import { Navigate } from "react-router-dom"
const About = React.lazy(() => import("../pages/About"))
const Login = React.lazy(() => import("../pages/Login"))
const routes = [
{
path: "/",
element: ,
},
{
path: "/",
element: ,
children: {
{ path: "/home", element: },
{ path: "/home/recommend", element: },
{ path: "/home/ranking", element: },
}
},
{
path: "/detail/:id",
element: ,
},
{
path: "*",
element: ,
}
]
export default routes
// src/index.js
import { Suspense } from 'react'
loading...}>
// App.jsx
import routes from @/router/index.js
{ useRoutes(routes) }