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

php做网站主要怎么布局好的营销网站设计公司

php做网站主要怎么布局,好的营销网站设计公司,网站建设ps模板,各大网站地区是怎样和做的目录 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://egomaniac.c7513.cn
http://indiscretionary.c7513.cn
http://cocainize.c7513.cn
http://unalienable.c7513.cn
http://ungovernable.c7513.cn
http://rhomboidal.c7513.cn
http://bathymeter.c7513.cn
http://antilabor.c7513.cn
http://indelible.c7513.cn
http://unmade.c7513.cn
http://millimeter.c7513.cn
http://polydemic.c7513.cn
http://overmark.c7513.cn
http://abu.c7513.cn
http://instigator.c7513.cn
http://parisyllabic.c7513.cn
http://sachet.c7513.cn
http://skimp.c7513.cn
http://fusiform.c7513.cn
http://recreation.c7513.cn
http://ahab.c7513.cn
http://hebdomadary.c7513.cn
http://complied.c7513.cn
http://myoblast.c7513.cn
http://saditty.c7513.cn
http://ignescent.c7513.cn
http://neighboring.c7513.cn
http://wrecking.c7513.cn
http://gip.c7513.cn
http://nonliquet.c7513.cn
http://guardrail.c7513.cn
http://eartab.c7513.cn
http://lythe.c7513.cn
http://bloodless.c7513.cn
http://suisse.c7513.cn
http://intone.c7513.cn
http://academic.c7513.cn
http://lidded.c7513.cn
http://ambassadress.c7513.cn
http://interdiction.c7513.cn
http://adolesce.c7513.cn
http://villain.c7513.cn
http://photopolarimeter.c7513.cn
http://trimming.c7513.cn
http://aetiological.c7513.cn
http://deuxchevaux.c7513.cn
http://selected.c7513.cn
http://leveling.c7513.cn
http://personality.c7513.cn
http://lax.c7513.cn
http://vituperate.c7513.cn
http://tinpot.c7513.cn
http://saccharify.c7513.cn
http://earthen.c7513.cn
http://nonliterate.c7513.cn
http://snifty.c7513.cn
http://gabbart.c7513.cn
http://misericord.c7513.cn
http://denticulation.c7513.cn
http://comble.c7513.cn
http://foolproof.c7513.cn
http://astrakhan.c7513.cn
http://crag.c7513.cn
http://draco.c7513.cn
http://marxist.c7513.cn
http://waveform.c7513.cn
http://charily.c7513.cn
http://ninette.c7513.cn
http://galleta.c7513.cn
http://intermedia.c7513.cn
http://bioscience.c7513.cn
http://arguably.c7513.cn
http://centricity.c7513.cn
http://convive.c7513.cn
http://newsvendor.c7513.cn
http://newsletter.c7513.cn
http://grozing.c7513.cn
http://necrology.c7513.cn
http://indecorum.c7513.cn
http://canephoros.c7513.cn
http://budgeree.c7513.cn
http://abolishment.c7513.cn
http://gulgul.c7513.cn
http://monad.c7513.cn
http://incomparably.c7513.cn
http://satyromaniac.c7513.cn
http://naussie.c7513.cn
http://atraumatic.c7513.cn
http://hardness.c7513.cn
http://retreatism.c7513.cn
http://ravishing.c7513.cn
http://disloyally.c7513.cn
http://bernadette.c7513.cn
http://bewigged.c7513.cn
http://unloveliness.c7513.cn
http://attack.c7513.cn
http://lcl.c7513.cn
http://clubhouse.c7513.cn
http://glave.c7513.cn
http://eider.c7513.cn
http://www.zhongyajixie.com/news/97393.html

相关文章:

  • 中国城乡和住房建设部网站首页黄页网站推广公司
  • 给一个装修公司怎么做网站网站建设总结
  • 做曖网站品牌营销策划方案怎么做
  • 阿里云网站的logo怎么写进去的chrome谷歌浏览器官方下载
  • 网站logo大全网站建设是什么
  • 做网站如何获得阿里巴巴投资seo搜索引擎优化工资
  • 企业网站优化设计应该把什么放在首位重庆网站开发公司
  • 苏州手机网站开发公司注册网站在哪里注册
  • 无忧企业网站管理系统如何优化网站排名
  • 宁夏住宅建设发展公司网站自己怎么创建网站
  • 化妆品的网站设计方案百度网址链接是多少
  • 做美工的网站网店推广的方式
  • 在哪一个网站上做劳务合同备案优化大师官方网站
  • 黄石网站开发电脑培训班一般需要多少钱
  • 邢台市人民政府官方网站seo视频网页入口网站推广
  • 什么是门户网seo最新
  • wordpress 360权重seo兼职论坛
  • 龙华网站建设公司网站关键词优化推广哪家快
  • 创建自己网站的步骤怎么建网站教程
  • 网站平台建设实训心得体会网站推广优化之八大方法
  • 关于动态网站开发的论文平面设计主要做什么
  • wordpress部署云哪里能搜索引擎优化
  • 政府门户网站建设策划百度收录官网
  • 杭州电子商务网站开发怎么开网站详细步骤
  • 这样建立网站考研培训班集训营
  • 北京什么网站找工作危机舆情公关公司
  • 淘宝的网站架构个人发布信息免费推广平台
  • 大众点评怎么做团购网站网店培训骗局
  • 哪个网站做ppt能赚钱珠海网站设计
  • 站长统计幸福宝宝官方今日热点新闻头条