NoteZ_技术博客 NoteZ_技术博客
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)

NoteZ_技术博客

前端界的小学生
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)
  • JavaScript笔记

  • CSS笔记

  • HTML笔记

  • Canvas笔记

  • Nuxt笔记

  • React笔记

    • React classnames 的使用方法
    • React de条件渲染
    • React 中 Hook 使用规则
    • React 中常用的 Hook 使用方法
    • React 元素渲染
    • React 自定义 Hook
    • React_16路由配置方法
      • 安装依赖
      • 引入路由组件
      • 编写一个简单路由
      • 路由动态传值
      • 路由重定向
    • React中的 State 与 生命周期
    • React中的事件处理
    • React中组件与Props
    • React使用State Hook
    • React技术框架
    • React组合与继承
    • React获取元素动态高度
    • Vue 或 React 中判断图片链接失效加载默认图
    • 受控组件与非受控组件
    • 如何使用Effect Hook
    • 如何将 React 项目部署到 github 的 gh-pages 分支上
    • 浅谈React受控组件与非受控组件
    • 自定义Hooks获取窗口大小
  • Vue笔记

  • TypeScript笔记

  • AI相关笔记

  • 开发文档
  • React笔记
NoteZ
2020-04-03
目录

React_16路由配置方法

# 安装依赖

npm i react-router react-router-dom --save
1

# 引入路由组件

import {BrowserRouter as Router,Route,Link,Redirect} from 'react-router-dom'
1

# 编写一个简单路由

在src目录下建立一个AppRouter.js文件,然后写入下面的代码:

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
function Index() {
  return <h2>我是首页</h2>;
}

function List() {
  return <h2>我是列表页</h2>;
}

function AppRouter() {
  return (
    <Router>
        <ul>
            <li> <Link to="/">首页</Link> </li>
            <li><Link to="/list/">列表</Link> </li>
        </ul>
        <Route path="/" exact component={Index} />
        <Route path="/list/" component={List} />
    </Router>
  );
}
export default AppRouter;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

改写src文件目录下的index.js代码:

import React from 'react';
import ReactDOM from 'react-dom'
import AppRouter from './AppRouter'

ReactDOM.render(<AppRouter/>,document.getElementById('root'))
1
2
3
4
5

# 路由动态传值

方式一

<Link to="/a/1">首页</Link>
<Route path="/a/:id" exact component={Index} />

import React, { Component } from 'react';
class List extends Component {
    constructor(props) {
        super(props);
        this.state = {  }
    }
    componentDidMount(){
        //console.log(this.props.match)
        // console.log(this.props.match.params.id)
        let tempId=this.props.match.params.id
        this.setState({id:tempId })
    }
    render() { 
        return (  <h2>List Page</h2> );
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

方式二

<Link to="/a?id=1">首页</Link>
<Route path="/a" exact component={Index} />

import React, { Component } from 'react';
class List extends Component {
    constructor(props) {
        super(props);
        this.state = {  }
    }
    componentDidMount(){
        let tempId=this.props.location.query.id
        this.setState({id:tempId })
    }
    render() { 
        return (  <h2>List Page</h2> );
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 路由重定向

标签式重定向

import { Redirect } from "react-router-dom";

// 引入Redirect后,直接在render函数里使用就可以了
<Redirect to="/home"/>    
1
2
3
4

编程式重定向

this.props.history.push("/home");  
1
#React
上次更新: 2024/01/30, 00:35:17
React 自定义 Hook
React中的 State 与 生命周期

← React 自定义 Hook React中的 State 与 生命周期→

最近更新
01
Gitea数据备份与还原
03-10
02
Linux 中使用 rsync 同步文件目录教程
03-10
03
Linux 使用 rsync 互相传输同步文件的简单步骤
03-08
更多文章>
Theme by Vdoing | Copyright © 2019-2025 NoteZ,All rights reserved | 冀ICP备2021027292号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式