中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

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

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

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

目錄

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

1,作用

之前在介紹 2.3 match 對象 時,提到了 react-router 使用第3方庫 path-to-regexp 來匹配路徑正則。

我們也利用它(版本v6.2.2),來手動實(shí)現(xiàn)一個獲取類似 match 對象的方法。

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

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

  • 不匹配時,返回 null。
  • 匹配時,返回一個對象

比如對下面的路由組件來說,

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

當(dāng)訪問 http://localhost:5173/news/123 時,返回的對象:

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

2.2,注意點(diǎn)

先做個測試,看下返回內(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 一個正則對象,

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

result 匹配的結(jié)果,

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

keys 的結(jié)果,

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

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

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

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

此時訪問 http://localhost:5173/news/123/xxx 是并不匹配,matchnull。

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

在這里插入圖片描述

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

import { pathToRegexp } from "path-to-regexp";/*** 返回一個類似 match 的對象。* @param {*} path 路徑規(guī)則* @param {*} pathname 真實(shí)的地址* @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, // 嚴(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;
}

測試1,

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

當(dāng)訪問 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);

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

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

以上。

http://www.risenshineclean.com/news/31120.html

相關(guān)文章:

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