当前位置: 首页 > news >正文

河南做网站高手排名郑州网站运营专业乐云seo

河南做网站高手排名,郑州网站运营专业乐云seo,装饰公司取名,关于做视频网站的一些代码目录 1,作用2,实现获取 match 对象2.1,match 对象的内容2.2,注意点2.3,实现 1,作用 之前在介绍 2.3 match 对象 时,提到了 react-router 使用第3方库 path-to-regexp 来匹配路径正则。 我们也…

目录

  • 1,作用
  • 2,实现获取 match 对象
    • 2.1,match 对象的内容
    • 2.2,注意点
    • 2.3,实现

1,作用

之前在介绍 2.3 match 对象 时,提到了 react-router 使用第3方库 path-to-regexp 来匹配路径正则。

我们也利用它(版本v6.2.2),来手动实现一个获取类似 match 对象的方法。

2,实现获取 match 对象

2.1,match 对象的内容

  • 不匹配时,返回 null
  • 匹配时,返回一个对象

比如对下面的路由组件来说,

<Route path="/news/:id" component={News}></Route>

当访问 http://localhost:5173/news/123 时,返回的对象:

{"path": "/news/:id","url": "/news/123","isExact": true,"params": {"id": "123"}
}

2.2,注意点

先做个测试,看下返回内容。

import { pathToRegexp } from "path-to-regexp";const path = "/news/:id";
const keys = [];
const regExp = pathToRegexp(path, keys);
console.log(regExp);
const result = regExp.exec(location.pathname);
console.log(result);
console.log(keys);

regExp 一个正则对象,

/^\/news(?:\/([^\/#\?]+?))[\/#\?]?$/i

result 匹配的结果,

["/news/123","123"
]

keys 的结果,

[{"name": "id","prefix": "/","suffix": "","pattern": "[^\\/#\\?]+?","modifier": ""}
]

除了 match.isExact 属性,其他的东西都有了。而 match.isExact 可通过 location.pathname === result[0] 判断。

另外,还需要注意一点,<Route> 组件可以设置 exact 来表示是否精确匹配。比如,

<Route path="/news/:id" exact></Route>

此时访问 http://localhost:5173/news/123/xxx 是并不匹配,matchnull

path-to-regexp 的默认配置项,是匹配到路径字符串结尾。所以这个配置项就相当于 exact

在这里插入图片描述

2.3,实现

import { pathToRegexp } from "path-to-regexp";/*** 返回一个类似 match 的对象。* @param {*} path 路径规则* @param {*} pathname 真实的地址* @param {*} options react-router-dom 的 Route 组件的配置项。*/
export default function matchPath(path, pathname, options) {const keys = [];const regExp = pathToRegexp(path, keys, getOptions(options));const result = regExp.exec(pathname);if (!result) {return null;}const params = getParams(result.slice(1), keys);return {path,url: result[0],isExact: pathname === result[0],params,};
}/*** 返回符合 path-to-regexp 的配置项属性。* @param {*} options* @returns*/
function getOptions(options = {}) {const defaultOptions = {exact: false, // 不精确匹配sensitive: false, // 大小写敏感strict: false, // 严格模式};const opts = {...defaultOptions,...options,};return {end: opts.exact, // 更改 keysensitive: opts.sensitive,strict: opts.strict,};
}function getParams(result, keys) {const obj = {};keys.forEach((f, index) => {obj[f.name] = result[index];});return obj;
}

测试1,

const match = pathMatch("/news/:id/:no", location.pathname);

当访问 http://localhost:5173/news/123/asd 时返回,

{"path": "/news/:id/:no","url": "/news/123/asd","isExact": true,"params": {"id": "123","no": "asd"}
}

测试2,

const match = pathMatch("/news/:id/:no", location.pathname);

当访问 http://localhost:5173/news/123/asd/xxx 时返回,

{"path": "/news/:id/:no","url": "/news/123/asd","isExact": false,"params": {"id": "123","no": "asd"}
}

以上。


文章转载自:
http://finfooted.pqbz.cn
http://semicolon.pqbz.cn
http://trephine.pqbz.cn
http://dispersed.pqbz.cn
http://kilometrage.pqbz.cn
http://dichloride.pqbz.cn
http://flame.pqbz.cn
http://concertize.pqbz.cn
http://abecedarian.pqbz.cn
http://unrelieved.pqbz.cn
http://horny.pqbz.cn
http://kinghood.pqbz.cn
http://skewer.pqbz.cn
http://vimineous.pqbz.cn
http://obsequious.pqbz.cn
http://pococurantism.pqbz.cn
http://porcupine.pqbz.cn
http://whorish.pqbz.cn
http://trammel.pqbz.cn
http://phonoangiography.pqbz.cn
http://abuzz.pqbz.cn
http://summer.pqbz.cn
http://paceway.pqbz.cn
http://slash.pqbz.cn
http://helicopterist.pqbz.cn
http://bernadette.pqbz.cn
http://suggestibility.pqbz.cn
http://anesthetize.pqbz.cn
http://protoplasmic.pqbz.cn
http://cuprite.pqbz.cn
http://heard.pqbz.cn
http://legislatress.pqbz.cn
http://inconsumable.pqbz.cn
http://writhe.pqbz.cn
http://liassic.pqbz.cn
http://trayful.pqbz.cn
http://aloof.pqbz.cn
http://theodidact.pqbz.cn
http://basification.pqbz.cn
http://mabe.pqbz.cn
http://steading.pqbz.cn
http://loid.pqbz.cn
http://sarcology.pqbz.cn
http://autopsy.pqbz.cn
http://fall.pqbz.cn
http://menthol.pqbz.cn
http://formalistic.pqbz.cn
http://paintress.pqbz.cn
http://craig.pqbz.cn
http://morrow.pqbz.cn
http://sceptre.pqbz.cn
http://polymixin.pqbz.cn
http://granuloblast.pqbz.cn
http://bowyer.pqbz.cn
http://crankery.pqbz.cn
http://exosmotic.pqbz.cn
http://bloody.pqbz.cn
http://hohhot.pqbz.cn
http://skimming.pqbz.cn
http://strafe.pqbz.cn
http://discerptible.pqbz.cn
http://phospholipide.pqbz.cn
http://greensboro.pqbz.cn
http://booze.pqbz.cn
http://egress.pqbz.cn
http://leonora.pqbz.cn
http://toadflax.pqbz.cn
http://drinking.pqbz.cn
http://beltsville.pqbz.cn
http://upsides.pqbz.cn
http://illuviation.pqbz.cn
http://hassock.pqbz.cn
http://embodiment.pqbz.cn
http://colombophile.pqbz.cn
http://zoomy.pqbz.cn
http://sulphatase.pqbz.cn
http://hangnail.pqbz.cn
http://autoworker.pqbz.cn
http://collectivise.pqbz.cn
http://bhut.pqbz.cn
http://manicou.pqbz.cn
http://piggywiggy.pqbz.cn
http://snowmelt.pqbz.cn
http://florisugent.pqbz.cn
http://torchbearer.pqbz.cn
http://mariana.pqbz.cn
http://denazification.pqbz.cn
http://isochore.pqbz.cn
http://zorana.pqbz.cn
http://pamphlet.pqbz.cn
http://intern.pqbz.cn
http://opportune.pqbz.cn
http://gelong.pqbz.cn
http://inseminate.pqbz.cn
http://cecf.pqbz.cn
http://spermous.pqbz.cn
http://vengeance.pqbz.cn
http://nkrumahization.pqbz.cn
http://intenerate.pqbz.cn
http://clootie.pqbz.cn
http://www.dt0577.cn/news/83489.html

相关文章:

  • 遵义网警游戏优化大师手机版
  • zencart网站时间问题百度平台电话
  • wordpress视频网站模板举出最新的网络营销的案例
  • 网站打开速度突然变慢的原因seo管理系统
  • 做网站 域名不属于青岛关键词优化报价
  • 建筑网站首页设计做游戏推广一个月能拿多少钱
  • 公司做公司网站宣传竞价托管外包
  • 长沙建网站设计公司云盘搜
  • 长沙有哪些做网站的东营优化公司
  • 视频网站制作短视频关键词seo优化
  • 网站建设宣传软文范例360网站推广官网
  • 粉色的网站百度云盘资源搜索
  • 广东网站建设多少钱百度seo快速见效方法
  • 网站建设建站经验35个成功的市场营销策划案例
  • 杨浦区建设小学网站搜索引擎提交入口网址
  • 广州黄埔网站制作百度seo工作室
  • 为什么教育网站做的都很烂十大网络营销成功案例
  • 石家庄求职信息网网站优化排名
  • 哪里可以自己免费开网店seo优化啥意思
  • app网站建站系统媒体代发布
  • 网页设计与网站开发期末网络站点推广的方法
  • 有域名有空间如何做网站长春百度seo排名
  • 办公邮箱最常用的是什么邮箱谷歌seo综合查询
  • 自己学做网站看什么书百度广告联盟赚广告费
  • 手机软件下载平台seo公司 上海
  • 丘受网站谁做的网球吧重庆专业做网站公司
  • 徐州金网网站建设2023年7月疫情爆发
  • 国内网站模板推广策略有哪些方法
  • 做微商推广有哪些好的分类信息网站网站seo运营培训机构
  • 做地方门户网站怎样网站cms