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

如何设计网站的首页门户网站建设和运行保证的磋商文件

如何设计网站的首页,门户网站建设和运行保证的磋商文件,百度推广好不好做,关键词搜索工具有哪些目录 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://www.yayakq.cn/news/816026/

相关文章:

  • 陕西省建设执业中心网站网页游戏梦幻西游
  • 什么是网站源码天津智能网站建设方案
  • 中国互联网协会官方网站百度全网营销
  • 网站后台模板htmldjango网站开发实例pdf
  • 做任务赚佣金网站有哪些电子邮箱怎么填写
  • 公司商城网站建设方案机械加工网上找订单
  • 大型网站建设兴田德润简介天河建设网站外包
  • html5网站开发费用装修门面一般找谁
  • 怎样制作网站平台html打开网页播放视频
  • asp 网站源码中小企业经营管理培训班
  • 国外移动网站设计接入网站备案要多久
  • 违法网站开发利用vs做网站
  • 网站建设 建议政务服务网站建设技术因素
  • html做电子书网站云服务器和网站备案吗
  • 网站建设企业建站方案软件开发公司经营范围
  • 恩施网站建设深圳华强北鬼市
  • 网站建设服务哪家好上海装修公司排名30
  • 什么网站可以学习建设工程法律实践杭州建设招聘信息网站
  • 山西建设监理协会官方网站网站加载速度慢
  • 冷水滩城乡建设局网站百度有做企业网站吗
  • 网站设计 广州网站域名免费申请
  • 做网站销售 优帮云济南网站设计报价
  • 北京网站关键词优化公wordpress环境配置
  • 类似于wordpress的网站吗支付宝小程序
  • 什么网站做的好看的flash网站片头动画
  • 闵行网站建设公司深圳市律师网站建设怎么样
  • 培训机构网站设计大连网站制作仟亿科技
  • 网站建设中搜索引擎的作用设计中国北京官网
  • 做旅游网站图片哪里找直播app
  • 哪些网站可以做设计软件亿恩 网站备案