import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  
  {
    path: '/login',
    name: '登录',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: '登录',
    component: () => import('../views/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('../views/main.vue'),
    children:[
      {
        path: 'about',
        name: 'About',
        component: () => import('../views/About.vue')
      },
    ],
  },
  {
    path: '/main/*',
    name: 'main',
    component: () => import('../views/main.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const childRoute = ['/app-vue-hash','/app-vue-history','/main/fjhxCloudVue'];
const isChildRoute = path => childRoute.some(item => path.startsWith(item))
const rawAppendChild = HTMLHeadElement.prototype.appendChild;
const rawAddEventListener = window.addEventListener;
router.beforeEach((to, from, next) => {
  // 从子项目跳转到主项目
  if(isChildRoute(from.path) && !isChildRoute(to.path)){
    HTMLHeadElement.prototype.appendChild = rawAppendChild;
    window.addEventListener = rawAddEventListener;
  }
  next();
});

export default router
