Function updateQueryParams

  • 更新当前页面的 URL 查询参数

    支持单个键值对或传入对象进行批量更新,更新后不刷新页面,仅修改地址栏内容。 如果某个参数的值为 undefinednull,该参数将被从 URL 中移除。

    Parameters

    • key: string | Record<string,
          | undefined
          | null
          | string
          | number>

      查询参数的键,或包含多个参数的对象

    • Optionalvalue: null | string | number

      查询参数的值,仅在 key 为字符串时使用

    Returns string

    返回更新后的完整 URL 字符串

    updateQueryParams("page", 2);
    // URL 变为 "...?page=2"
    updateQueryParams({ page: 1, keyword: "test" });
    // URL 变为 "...?page=1&keyword=test"
    updateQueryParams("page", null);
    // 删除 page 参数