国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

php做網(wǎng)站主要怎么布局百度明星人氣排行榜

php做網(wǎng)站主要怎么布局,百度明星人氣排行榜,鞏義網(wǎng)站,免費(fèi)個(gè)人網(wǎng)站哪個(gè)好目錄 1,作用2,實(shí)現(xiàn)獲取 match 對(duì)象2.1,match 對(duì)象的內(nèi)容2.2,注意點(diǎn)2.3,實(shí)現(xiàn) 1,作用 之前在介紹 2.3 match 對(duì)象 時(shí),提到了 react-router 使用第3方庫(kù) path-to-regexp 來(lái)匹配路徑正則。 我們也…

目錄

  • 1,作用
  • 2,實(shí)現(xiàn)獲取 match 對(duì)象
    • 2.1,match 對(duì)象的內(nèi)容
    • 2.2,注意點(diǎn)
    • 2.3,實(shí)現(xiàn)

1,作用

之前在介紹 2.3 match 對(duì)象 時(shí),提到了 react-router 使用第3方庫(kù) path-to-regexp 來(lái)匹配路徑正則。

我們也利用它(版本v6.2.2),來(lái)手動(dòng)實(shí)現(xiàn)一個(gè)獲取類(lèi)似 match 對(duì)象的方法。

2,實(shí)現(xiàn)獲取 match 對(duì)象

2.1,match 對(duì)象的內(nèi)容

  • 不匹配時(shí),返回 null。
  • 匹配時(shí),返回一個(gè)對(duì)象

比如對(duì)下面的路由組件來(lái)說(shuō),

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

當(dāng)訪問(wèn) http://localhost:5173/news/123 時(shí),返回的對(duì)象:

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

2.2,注意點(diǎn)

先做個(gè)測(cè)試,看下返回內(nèi)容。

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 一個(gè)正則對(duì)象,

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

result 匹配的結(jié)果,

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

keys 的結(jié)果,

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

除了 match.isExact 屬性,其他的東西都有了。而 match.isExact 可通過(guò) location.pathname === result[0] 判斷。

另外,還需要注意一點(diǎn),<Route> 組件可以設(shè)置 exact 來(lái)表示是否精確匹配。比如,

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

此時(shí)訪問(wèn) http://localhost:5173/news/123/xxx 是并不匹配,matchnull。

path-to-regexp 的默認(rèn)配置項(xiàng),是匹配到路徑字符串結(jié)尾。所以這個(gè)配置項(xiàng)就相當(dāng)于 exact。

在這里插入圖片描述

2.3,實(shí)現(xiàn)

import { pathToRegexp } from "path-to-regexp";/*** 返回一個(gè)類(lèi)似 match 的對(duì)象。* @param {*} path 路徑規(guī)則* @param {*} pathname 真實(shí)的地址* @param {*} options react-router-dom 的 Route 組件的配置項(xiàng)。*/
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 的配置項(xiàng)屬性。* @param {*} options* @returns*/
function getOptions(options = {}) {const defaultOptions = {exact: false, // 不精確匹配sensitive: false, // 大小寫(xiě)敏感strict: false, // 嚴(yán)格模式};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;
}

測(cè)試1,

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

當(dāng)訪問(wèn) http://localhost:5173/news/123/asd 時(shí)返回,

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

測(cè)試2,

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

當(dāng)訪問(wèn) http://localhost:5173/news/123/asd/xxx 時(shí)返回,

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

以上。

http://m.aloenet.com.cn/news/31120.html

相關(guān)文章:

  • 美容產(chǎn)品網(wǎng)站建設(shè)多少錢(qián)seo診斷網(wǎng)站
  • 手機(jī)h5頁(yè)面制作教程關(guān)鍵詞seo排名怎么選
  • 東京購(gòu)物商城百度整站優(yōu)化
  • 技術(shù)支持 鄭州做網(wǎng)站企業(yè)網(wǎng)搭建
  • 旅游網(wǎng)站的導(dǎo)航怎么做陜西網(wǎng)站設(shè)計(jì)
  • 南寧網(wǎng)站建設(shè)醉懂網(wǎng)絡(luò)外鏈官網(wǎng)
  • 企業(yè)網(wǎng)站建設(shè)服務(wù)公司搜索引擎優(yōu)化工具
  • 北京建站推廣全網(wǎng)推廣公司
  • b2c網(wǎng)站怎么做google關(guān)鍵詞搜索技巧
  • 網(wǎng)站平臺(tái)免費(fèi)游戲推廣員每天做什么
  • 學(xué)校網(wǎng)站建設(shè)調(diào)研報(bào)告天津疫情最新情況
  • 做網(wǎng)站平臺(tái)營(yíng)銷(xiāo)網(wǎng)站建站公司
  • 哪些網(wǎng)站專(zhuān)門(mén)做細(xì)胞的好的搜索引擎推薦
  • wordpress評(píng)論框加游戲優(yōu)化大師官方下載
  • 中英網(wǎng)站開(kāi)發(fā)青島官網(wǎng)seo方法
  • 網(wǎng)站測(cè)試頁(yè)面怎么做網(wǎng)站推廣途徑和推廣要點(diǎn)
  • 天津門(mén)戶(hù)網(wǎng)站開(kāi)發(fā)國(guó)家免費(fèi)技能培訓(xùn)官網(wǎng)
  • 睢縣網(wǎng)站建設(shè)如何創(chuàng)建一個(gè)app平臺(tái)
  • 新公司注冊(cè)在哪個(gè)網(wǎng)站全國(guó)31省市疫情最新消息今天
  • 做網(wǎng)站用什么工具百度推廣四川成都地區(qū)服務(wù)中心
  • 做網(wǎng)站要學(xué)java嗎網(wǎng)站seo 工具
  • 新公司注冊(cè)工商核名系統(tǒng)網(wǎng)站優(yōu)化外包
  • 高端用戶(hù)群瀏覽網(wǎng)站石家莊網(wǎng)站seo
  • 西安手機(jī)網(wǎng)站建設(shè)公司排名查關(guān)鍵詞熱度的網(wǎng)站
  • 怎樣建立一個(gè)營(yíng)銷(xiāo)的公司網(wǎng)站抖音seo搜索優(yōu)化
  • 卓越 網(wǎng)站建設(shè) 深圳西鄉(xiāng)競(jìng)價(jià)托管
  • 電子商務(wù)網(wǎng)站規(guī)劃的原則是什么seo技術(shù)博客
  • 全國(guó)網(wǎng)站制作公司石家莊seo外包公司
  • 網(wǎng)站開(kāi)發(fā)周期石家莊關(guān)鍵詞排名提升
  • 海淀網(wǎng)站建設(shè)公司廣告推廣賺錢(qián)