vue-router3 源码注释系列 /src/util/path.js

/* @flow */

/**
 *  resolvePath(): 解析路径
 *  第一个参数: 相对路径,要跳转路径的 pathname。
 *  第二个参数: 基准路径。
 *  第三个参数: 是否需要拼接基准地址。
 */
export function resolvePath(
  relative: string,
  base: string,
  append?: boolean
): string {
  //判断 relative 是否是以 “/” 开头。
  const firstChar = relative.charAt(0)
  if (firstChar === '/') {
    //如果是,则认为是绝对路径,不需要拼接基准路径。
    return relative
  }

  //如果以 ? 或者 # 开头,则表示要跳转的路径是 "",则表示是原路径跳转,即刷新本页面。
  //  所以拼接原来路径的 pathName。
  if (firstChar === '?' || firstChar === '#') {
    return base + relative
  }

  /*
     下面的路径是相对路径的情形
  */

  //将 base 路径按照 "/" 切分成数组。
  const stack = base.split('/')

  // remove trailing segment if:
  // - not appending
  // - appending to trailing slash (last segment is empty)

  //如果需要追加基础路径,且 stack 最后一个元素为 “”,则将最后一个元素移除。
  if (!append || !stack[stack.length - 1]) {
    stack.pop()
  }

  //relative.replace(/^\//, ''): 去除开头的第一个 /.
  //然后按照 ‘/’ 进行切割成数组。
  const segments = relative.replace(/^\//, '').split('/')
  for (let i = 0; i < segments.length; i++) {
    const segment = segments[i]
    //如果是 '..', 则表示当前目录的上一级目录。
    if (segment === '..') {
      //则弹出当前目录代表的元素。
      stack.pop()
    } else if (segment !== '.') {
      //如果是 '.', 则表示是当前目录,不需要处理。
      //否则就是有效路径。被添加到 stack 中。
      stack.push(segment)
    }
  }

  //之所以要添加一个 '' 在数组的队首,是为了保证 stack.join('/') 是, 是 "" + “/” +... 的情形。
  //即字符串以 “/”开头。
  if (stack[0] !== '') {
    stack.unshift('')
  }

  //含有路径信息的数组,转为 “/” 间隔的字符串形式。
  return stack.join('/')
}

/*
  parsePath():  拆分路径,解析成一个 { path, query, hash } 的对象。
  第一个参数: path 是 location.url; 或者 this.$router.push("/user/info?name=mengze#a",{ query: {xxx} } )
            中的 /user/info?name=mengze#a 这一部分内容。
  */
export function parsePath(path: string): {
  path: string,
  query: string,
  hash: string,
} {
  let hash = ''
  let query = ''
  //举一个路径的例子: /user/info?name=mengze&age=18#aaa

  //判断 path 上是否有 # 号
  const hashIndex = path.indexOf('#')
  //如果存在 # 号,则将 # 后面的内容记录为 hash。 且将 path 去除 # 之后的内容。
  if (hashIndex >= 0) {
    hash = path.slice(hashIndex)
    path = path.slice(0, hashIndex)
  }

  //判断 path 上是否有 ?号
  const queryIndex = path.indexOf('?')
  //如果存在 ? 号。。
  if (queryIndex >= 0) {
    //则将 ? 后面的内容记录为 query string
    query = path.slice(queryIndex + 1)
    //将 path 含有的 ?以及后面的字符串都去掉。保存的内容只有 window.location.href.pathname 部分。
    path = path.slice(0, queryIndex)
  }

  /*
    将 path 路径解析为一个 pathName, query,hash 分离的对象。此时的 query 还是个字符串。
  */
  return {
    path,
    query,
    hash,
  }
}

/**
 * 清理 path 路径中 // 的情形。
 */
export function cleanPath(path: string): string {
  //如果路径中存在连续的多个//,那么替换为只有一个 /。
  return path.replace(/\/+/g, '/')
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WARN "css.modules" option in vue.config.js is deprecated now, please use "css.requireModuleExtension" instead. INFO Starting development server... 98% after emitting CopyPlugin WARNING Compiled with 17 warnings 09:43:57 warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'computed' was not found in 'vue' warning in ./src/router/index.js "export 'default' (imported as 'VueRouter') was not found in 'vue-router' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'defineComponent' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'getCurrentInstance' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'h' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'inject' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'nextTick' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onActivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onDeactivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onUnmounted' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'provide' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'reactive' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'ref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'shallowRef' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'unref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watch' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watchEffect' was not found in 'vue'这个报错因为什么
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值