网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

区别React-Router中match的path和url

问题

React Router开发中有关组件的match属性的两个属性path和url,容易让人混淆,特别记录于此。

蒙自ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

解释

官方描述如下:

  • path - (string) The path pattern used to match. Useful for building nested s
  • url - (string) The matched portion of the URL. Useful for building nested s

path用来标识路由匹配的URL部分。React Router使用了Path-to-RegExp库将路径字符串转为正则表达式。然后正则表达式会匹配当前路径。

当路由路径和当前路径成功匹配,会生成一个对象match。match对象有更多关于URL和path的信息。这些信息可以通过它的属性获取,如下所示:

  • match.url.返回URL中匹配部分的字符串。用于创建嵌套的很有用。
  • match.path.用于匹配路径模式。用来创建嵌套的
  • match.isExact.返回布尔值,如果准确(没有任何多余字符)匹配则返回true。
  • match.params.返回一个对象包含Path-to-RegExp包从URL解析的键值对。

只有完全理解了的这个match对象属性及其有关属性,才能算是掌握了基本类型嵌套路由开发的基础部分(本人拙见,仅供参考)。

举例1

我们不妨考虑一个小例子,如下所示:

观察路由"/users/:userId"
此例中,match.path的返回值将是 "/users/:userId"。
而match.url 的返回值将是:userId的值,例如"users/5"。
请注意上面官方描述中,match.path指用于匹配的模式部分,代表了一种格式,而match.url代表一个具体的计算后的路径表达值。

举例2

根据上面的解释,match.path和match.url的值是相同的,此时你想使用哪一个都行。但是,本人建议还是遵循官方解释,在嵌套式组件中尽量使用match.url,而在嵌套式组件中尽量使用match.path。
从官方网站也会观察到上面混合使用情况。

在Recursive Paths部分,你会观察到如下代码:

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

const PEEPS = [
  { id: 0, name: "Michelle", friends: [1, 2, 3] },
  { id: 1, name: "Sean", friends: [0, 3] },
  { id: 2, name: "Kim", friends: [0, 1, 3] },
  { id: 3, name: "David", friends: [1, 2] }
];

const find = id => PEEPS.find(p => p.id == id);

const RecursiveExample = () => (
  
    
  
);

const Person = ({ match }) => {
  const person = find(match.params.id);

  return (
    

{person.name}’s Friends

    {person.friends.map(id => (
  • {find(id).name}
  • ))}
); }; export default RecursiveExample;

而在佳文https://www.sitepoint.com/react-router-v4-complete-guide/
中也使用了混合使用的情况(见“Demo 3: Nested routing with Path parameters”一节):

const Products = ({ match }) => {

const productsData = [
{
id: 1,
name: 'NIKE Liteforce Blue Sneakers',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin molestie.',
status: 'Available'

},

//Rest of the data has been left out for code brevity

];
/* Create an array of <li> items for each product
var linkList = productsData.map( (product) => {
return(


  • ${match.url}/${product.id}}>
    {product.name}


  • )

    })

    return(




    Products


      {linkList}


         }/>
         (
            
    Please select a product.
    )} />

    )
    }

    引用

    1.https://www.zcfy.cc/article/react-router-v4-the-complete-guide-mdash-sitepoint-4448.html
    2.https://teamtreehouse.com/community/what-is-the-difference-between-path-and-url-in-match-prop-of-reactrouter-route-component-react-router-basics
    3.https://reacttraining.com/react-router/


    本文名称:区别React-Router中match的path和url
    链接URL:http://bjjierui.cn/article/gdphsp.html

    其他资讯