import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);
//登录 注册 激活
const page = [
  {
    path: "/",
    redirect: "/login" //调试
  },
  {
    path: "/login",
    meta: { index: 1, keepAlive: false },
    name: "login",
    component: () => import("@/pages/login")
  },
  {
    path: "/peopleList",
    meta: { index: 2, keepAlive: true, scrollTop: 0 },
    name: "peopleList",
    component: () => import("@/pages/peopleList")
  },
  {
    path: "/videoList",
    meta: { index: 3, keepAlive: true, scrollTop: 0 },
    name: "videoList",
    component: () => import("@/pages/videoList")
  },
  {
    path: "/videoShow",
    meta: { index: 3, keepAlive: false },
    name: "videoShow",
    component: () => import("@/pages/videoShow")
  }
];

const router = new Router({
  // scrollBehavior: () => ({
  //   y: 0
  // }),
  routes: [...page]
});

router.beforeEach((to, from, next) => {
  if (from.meta.keepAlive) {
    const $wrapper = document.querySelector(".list_content"); // 列表的外层容器 注意找到滚动的盒子
    const scrollTop = $wrapper ? $wrapper.scrollTop : 0;
    // console.log("scrollTop=", scrollTop);
    from.meta.scrollTop = scrollTop;
  }
  next();
});
// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = Router.prototype.push;
const originalReplace = Router.prototype.replace;

// push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

//replace
Router.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch((err) => err);
};
export default router;
