All files / mp getCurrentPagePath.ts

100% Statements 4/4
75% Branches 3/4
100% Functions 2/2
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                          2x 2x 2x     2x      
import { AnyObject } from '../types'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { last } from '../utils'
 
/**
 * 获取当前页面的路径(不含查询参数),始终以 `/` 开头。
 *
 * @param pageInstance 页面实例,默认当前页面
 * @returns 返回当前页面的路径
 */
export function getCurrentPagePath(
  pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>,
): string {
  return ensureInMiniProgram(() => {
    pageInstance = pageInstance || last(getCurrentPages())!
    const path = `/${
      pageInstance.route || /* 字节跳动 */ pageInstance.__route__
    }`.replace(/\/{2,}/g, '/')
    return path
  })
}