import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './defaultRoute'
// import Layout from '@/layout'
Vue.use(VueRouter)

// 重写 VueRouter 的 push 方法
const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => {}, () => {})
  }
}

const createRouter = () => new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // 使用浏览器的回退或者前进时，重新返回时保留页面滚动位置，跳转页面的话，不触发。
  scrollBehavior (to, from, savePosition) {
    if (savePosition) {
      return savePosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

const router = createRouter()

export function resetRouter () {
  router.matcher = createRouter().matcher // 重置路由
}

export default router
