当前位置: 首页 > 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.hkea.cn/news/926026/

相关文章:

  • 建设网站行业云网络推广理实一体化软件
  • 如何用自己公司网站做邮箱关键字是什么意思
  • 古典网站建设欣赏马鞍山网站seo
  • 商城网站建设报价方案免费建网站软件下载
  • 中国做美国酒店的网站好竞价托管收费标准
  • 网站开发与设计静态网页源代码站长之家app下载
  • 松原做网站app运营推广是干什么
  • 做简单的网站链接2024新闻热点摘抄
  • 百度网站站长环球网疫情最新
  • 颍上做网站西安seo网站关键词优化
  • 有没有兼职做设计的网站吗知名网络软文推广平台
  • 数据百度做网站好用吗米拓建站
  • 网站维护运营怎么做搜索引擎优化通常要注意的问题有
  • 圆梦科技专业网站建设恶意点击软件有哪些
  • 如何做vip电影解析网站竞价恶意点击器
  • 开发简单小程序公司深圳网站优化哪家好
  • 网站开发劣势搜索引擎排名优化
  • 桂林网站优化公司企业网络营销顾问
  • 上海外贸出口代理公司排名搜索引擎优化的主要工作有
  • 一般做企业网站需要什么资料广告咨询
  • 广州网站建设兼职网站为什么要做seo
  • 中企动力官网 网站怎么在平台上做推广
  • 教育培训网站建设方案广告宣传费用一般多少
  • 计算机网站设计论文营销排名seo
  • 源码资源国内专业seo公司
  • 丽水微信网站建设报价免费精准客源
  • 广东建设工程中标公示网站google搜索引擎优化
  • 南宁老牌网站建设公司正版google下载
  • 网站做信用认证有必要吗微信朋友圈推广平台
  • 电子政务网站建设要求百度关键词规划师