All files / mp getCurrentPageUrl.ts

100% Statements 6/6
50% Branches 1/2
100% Functions 2/2
100% Lines 6/6

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 23 24                              1x 1x 1x 1x 1x 1x      
import { AnyObject } from '../types'
import { createUrlQueryString } from '../utils'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { getCurrentPagePath } from './getCurrentPagePath'
import { getCurrentPageQuery } from './getCurrentPageQuery'
 
/**
 * 获取当前页面的地址(包含查询参数)。
 *
 * @param pageInstance 页面实例,默认当前页面
 * @returns 返回当前页面的地址
 */
export function getCurrentPageUrl(
  pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>,
): string {
  return ensureInMiniProgram(() => {
    const path = getCurrentPagePath(pageInstance)
    const query = getCurrentPageQuery(pageInstance)
    const queryString = createUrlQueryString(query)
    const url = `${path}${queryString ? `?${queryString}` : ''}`
    return url
  })
}