前言【vue-router源码】系列文章从0开始带你了解vue-router的具体实现。本系列源码文章参考vue-routerv4.0.15。源码地址:https://github.com/vuejs/router阅读本文的前提是你最好了解vue-router的基本使用。如果没有使用过,可以通过vue-router官网进行学习。本文将介绍router.resolve的实现。使用router.resolve方法返回路由器地址的规范化版本。router.resolve('admin')router.resolve({path:'/admin'})resolveresolve接收两个参数:rawLocation、currentLocation(可选)。其中,rawLocation是要转换的路由,rawLocation可以是一个对象,也可以是一个字符串。如果不传递currentLocation,则默认为currentRoute。resolve有两个分支:如果rawLocation是string类型,调用parseURL解析rawLocation:constlocationNormalized=parseURL(parseQuery,rawLocation,currentLocation.path)parseURL接收三个参数:parseQuery(查询解析函数),location(待解析函数)location),currentLocation(当前位置)。导出函数parseURL(parseQuery:(search:string)=>LocationQuery,location:string,currentLocation:string='/'):LocationNormalized{letpath:string|undefined,query:LocationQuery={},searchString='',hash=''//的位置?inlocationconstsearchPos=location.indexOf('?')//#在location中的位置,如果有?在location中,寻找#consthashPos=location.indexOf('#',searchPos>-1?searchPos:0)//ifif(searchPos>-1){//截取位置[0,searchPos)处的字符串fromlocationaspathpath=location.slice(0,searchPos)//从location截取包含search的字符串,不包括hash部分searchString=location.slice(searchPos+1,hashPos>-1?hashPos:location.length)//调用parseQuery生成查询对象query=parseQuery(searchString)}//如果位置有hashif(hashPos>-1){path=path||location.slice(0,hashPos)//从location截取[hashPos,location.length)作为hash(包括#)hash=location.slice(hashPos,location.length)}//解析以.开头的相对路径。路径=resolveRelativePath(路径!=空?路径:位置n,currentLocation)//空路径表示相对查询或哈希`?foo=f`,`#thing`return{//fullPath=path+searchString+hashfullPath:path+(searchString&&'?')+searchString+hash,path,query,hash,}}看一下相对路径的解析过程:绝对路径,直接return即if(to.startsWith('/'))returnto//如果from不以/开头,则说明from不是绝对路径,无法猜测绝对路径的。这时候直接返回toif(__DEV__&&!from.startsWith('/')){warn(`Cannotresolvearelativelocationwithoutanabsolutepath.Tryingtoresolve"${to}"from"${from}".它应该看起来像"/${from}".`)returnto}if(!to)returnfrom//use/splitfromandtoconstfromSegments=from.split('/')consttoSegments=to.split('/')//初始化position默认为fromSegmentsletposition=fromSegments.length-1lettoPosition:numberletsegment:stringfor(toPosition=0;toPosition
