React 路由基本配置、传值、编程式路由

在github官网上搜索react-router:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
一、路由配置

  1. 安装路由:cnpm install react-router-dom --save

  2. 引入import { BrowserRouter as Router, Route, Link } from "react-router-dom"

  3. 在组件根节点外面包裹一层<Router></Router>标签

  4. 根据路径跳转 <Route path="/news" component={News} /> 其中path是路径 ,component对应着组件(此时输入对应路径即可跳转到对应页面)

  5. <Link to="/new">News</Link> 用Link标签模拟一个人a标签,点击跳转

  • 路由安装完毕之后,配置文件:
import React from 'react';

//导入路由
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';

//导入路由子组件
import Home from './Home';
import News from './News';
import My from './My';

class Main extends React.Component{
    render(){
        return (
            <div>
                <Router>
                    {/* 路由的入口  属性:to*/}
                    <ul>
                        <li>
                            <Link to="/">Home</Link>
                        </li>
                        <li>
                            <Link to="/news">News</Link>
                        </li>
                        <li>
                            <Link to="/my">My</Link>
                        </li>
                    </ul>
                    <hr/>
                    {/* 配置路由  
                        属性:path component 
                        exact为Route的属性,为bool类型(true为严格匹配 false为普通匹配)
                    */}
                    <Route exact path="/" component={Home}></Route>
                    <Route path="/news" component={News}></Route>
                    <Route path="/my" component={My}></Route>
                </Router>
            </div>
        )
    }
}

export default Main;

二、传值

  1. 动态路由传值

(1)传单个值

Main.js:

import React from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
import News from './News';

class Main extends React.Component{
    constructor(props){
        super(props);
        this.state={
            id:66
        }
    }
    render(){
        return (
            <div>
                <Router>
					{/* 1. 固定传值 */}
                    <Link to="/news/1">News</Link>  
                    {/* 2. 拼接传值 */}
                    <Link to={'/news/'+this.state.id}>News</Link>  
                    {/* 3. ES6模板传值 */}
                    <Link to={`/news/${this.state.id}`}>News</Link>
                    <hr/>
                    <Route path="/news/:uid" component={News}></Route>
                </Router>
            </div>
        )
    }
}

export default Main;

News.js中取值:

import React from 'react';

class News extends React.Component {
    componentDidMount(){
        console.log(this.props.match.params.uid);
    }
    render() {
        return (
            <div>News</div>
        );
    }
}

export default News;

(2)传多个值

import React from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
import News from './News';

class Main extends React.Component{
    constructor(props){
        super(props);
        this.state={
            id:66,
            name:"易安"
        }
    }
    render(){
        return (
            <div>
                <Router>
                	<Link to={`/news/${this.state.id}/${this.state.name}`}>News</Link>
                    <hr/>
                    <Route path="/news/:uid/:uname" component={News}></Route>
                </Router>
            </div>
        )
    }
}

export default Main;
  1. get 传值(相当于在路径上加 ?id=1)
  • 路由的get传值,值在location的search,可以写算法进行解析,也可安装node的url模块进行解析。

Main.js:

import React from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
import My from './My';

class Main extends React.Component{
    render(){
        return (
            <div>
                <Router>
                	<Link to="/my?id=6&name=ikun">My</Link>
                    <Route path="/my" component={My}></Route>
                </Router>
            </div>
        )
    }
}

export default Main;

(1)算法解析

My.js:

import React from 'react';

class My extends React.Component {
    componentDidMount(){
        let locurl=this.props.location.search;
        this.changeUrl(locurl);
    }
    changeUrl=(locurl)=>{
        let u=locurl.replace("?","");
        let ur=u.replace(/=/g,":").replace("&",",");
        console.log(ur.split(",")[0].split(":")[1]);
    }
    render() {
        return (
            <div>My</div>
        );
    }
}

export default My;

(2)url模块解析

import React from 'react';
import url from 'url';

class My extends React.Component {
    componentDidMount(){
        let locurl=this.props.location.search;
        let data=url.parse(locurl,true).query;
        console.log(data);
    }
    render() {
        return (
            <div>My</div>
        );
    }
}

export default My;
  1. 数组中的数据进行传值

动态路由传值+get传值:

import React from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
import dyValue from './dyValue';
import getValue from './getValue';

class Main extends React.Component{
    constructor(props){
        super(props);
        this.state={
            arr:[
                {
                    aid:1,
                    title:"Vue"
                },
                {
                    aid:2,
                    title:"React"
                },
                {
                    aid:3,
                    title:"Node"
                }
            ]
        }
    }
    render(){
        return (
            <div>
                <Router>
                    <h3>动态路由传值</h3>
                    {
                        this.state.arr.map((value,index)=>{
                            return (
                                <li key={index}>
                                    <Link to={`/dyvalue/${value.aid}`}>{value.title}</Link>
                                </li>
                            )
                        })
                    }
                    <Route path="/dyvalue/:aid" component={dyValue}></Route>
                    <h3>get方式传值</h3>
                    {
                        this.state.arr.map((value,index)=>{
                            return (
                                <li key={index}>
                                    <Link to={`/getvalue?aid=${value.aid}`}>{value.title}</Link>
                                </li>
                            )
                        })
                    }
                    <Route path="/getvalue" component={getValue}></Route>
                </Router>
            </div>
        )
    }
}

export default Main;

在对应跳转页面参数值:

componentDidMount() {
	console.log(this.props.match.params.aid);   //动态路由取值
    console.log(this.props.location.search);  //get取值,取出来的是?aid=1这种格式,需手动转或引入url模块,进行路由解析
}

四、路由嵌套+编程式路由+传值

  • 路由的跳转:任何需要跳转的地方使用this.props.history.push('/path')就可以进行跳转
  • 参数的获取:使用this.props.match.params.xxx可以获取到当前路由的参数

News.js(父路由):

import React from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom';
import child1 from './child1';
import child2 from './child2';

class News extends React.Component {
    //News向Home路由传值
    toLocation=()=>{
    	//跳转时可以带参,跳转的页面获取值时可用this.props.location.state
        this.props.history.push("/",{id:99});
    }
    render() {
        return (
            <div>
                News
                <hr/>
                <button onClick={this.toLocation}>跳转</button>
                <Router>
                    <ul>
                        <li>
                            <Link to="/News/child1/86">child1</Link>
                        </li>
                        <li>
                            <Link to="/News/child2">child2</Link>
                        </li>
                    </ul>
                    <Route exact path="/News/child1/:id" component={child1}></Route>
                    <Route path="/News/child2" component={child2}></Route>
                </Router>
            </div>
        );
    }
}

export default News;

Home.js(News.js的同级路由):

import React from 'react';

class Home extends React.Component {
	//接收从News路由传递的id值
    componentDidMount(){
        console.log(this.props.location.state);
    }
    render() {
        return (
            <div>Home</div>
        );
    }
}

export default Home;

child1.js(子路由):

import React from 'react';

class child1 extends React.Component {
    toFather=()=>{
        this.props.history.push("/");
    }
    render() {
        return (
            <div>子路由1
                <button onClick={this.toFather}>子路由1跳转</button>
            </div>
        );
    }
}

export default child1;

child2.js(子路由):

import React from 'react';

class child2 extends React.Component {
	//子路由2跳转到子路由1
    toLocation=()=>{
        this.props.history.push("/News/child1/86");
    }
    render() {
        return (
            <div>子路由2
                <button onClick={this.toLocation}>子路由2跳转</button>
            </div>
        );
    }
}

export default child2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值