import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/devGetSession',  //开发时获取session作用
    name: 'devGetSession',
    component: () => import('../views/DevGetSession.vue')
  },
  {
    path: '/create',
    name: 'create',
    component: () => import('../views/create/create')
  },
  {
    path: '/replace',
    name: 'replace',
    component: () => import('../views/replace/replace')
  }
]

const router = new VueRouter({
  routes
})

/**
 *
 * to 表示将要跳转到的组件 (目标组件)
 * console.log(from); //(源组件)
 * next();
 * next 是一个函数
 * next() 进入下一个组件的钩子函数
 * next(false) 阻止跳转 中断导航
 * next("/login") 进入指定的组件的钩子函数
 */
// 路由守卫
router.beforeEach((to, from, next) => {
  if (production === false && store.state.sessionId == null && to.path != "/devGetSession") {
    //进入一个路由获取session，获取session后再进入主入口
    next("/devGetSession");
  } else {
    //跳转前设置title
    //window.document.title = to.meta.title;
    if (store.state.mainPage == 'create') {
      store.commit("edit", { mainPage: Math.random() });
      next('/create');
    } else if (store.state.mainPage == 'replace') {
      store.commit("edit", { mainPage: Math.random() });
      next('/replace');
    } else {
      next();
    }
    // next();
  }
  //to.matched.some(res=>{res.meta.isLogin}) 能够获取路由配置的参数

});

//跳转后设置scroll为原点
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});
export default router
