# pushState

# API 使用

$router.push(params)

# 源代码

push-state.js (opens new window)

import { inBrowser } from './dom'
import { genStateKey, setStateKey, getStateKey } from './state-key'
import { extend } from './misc'

// 检测是否支持 pushstate 特性
export const supportsPushState = /** ... */;

export function pushState (url?: string, replace?: boolean) {
  const history = window.history

  try {
    // replace 模式
    if (replace) {
      const stateCopy = extend({}, history.state)
      // key值为时间戳
      stateCopy.key = getStateKey()

      history.replaceState(stateCopy, '', url)
    } else {

        // push模式
      history.pushState({ key: setStateKey(genStateKey()) }, '', url)
    }
  } catch (e) {
    // 出错时的降级
    window.location[replace ? 'replace' : 'assign'](url)
  }
}

export function replaceState (url?: string) {
  pushState(url, true)
}
上次更新于: 9/18/2021, 1:33:45 AM