DEV Community

ChuangWANG
ChuangWANG

Posted on

20210504

Get the URL which shows in the explorer HTTP URL bar:

console.log(window.location.href)
完整url可以用 window.location.href
路由路径可以用 this.$route.path
路由路径参数 this.$route.params
Enter fullscreen mode Exit fullscreen mode

Change the URL by pushing a path:

this.$router.push('/test')
Enter fullscreen mode Exit fullscreen mode

A vue-router demo:
We assume you has vue-router.js in your html script src:

  const routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/test', component: Home}
  ]

  const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes, // `routes: routes` 的缩写
  })
  const app = Vue.createApp({})
  app.use(router)
  app.mount('#app')
Enter fullscreen mode Exit fullscreen mode

模块化编程中使用createWebHashHistory:

import { createRouter, createWebHashHistory } from 'vue-router'
import UserPost from './views/UserPost.vue'

export const router = createRouter({
  history: createWebHashHistory(),
  routes: [{ path: '/users/:username/posts/:postId', component: UserPost }],
})
Enter fullscreen mode Exit fullscreen mode

模块化编程中使用createWebHistory:

import { createRouter, createWebHistory } from 'vue-router'
import UserPost from './views/UserPost.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [{ path: '/users/:username/posts/:postId', component: UserPost }],
})
Enter fullscreen mode Exit fullscreen mode

The webmanager router:

/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue'
import Router from 'vue-router'
import http from '@/utils/httpRequest'
import { isURL } from '@/utils/validate'
import { clearLoginInfo } from '@/utils'
import store from '@/store'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  { path: '/login', component: _import('common/login'), name: 'login', meta: { /* title: '登录' */ } }
]

// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '主入口整体布局' },
  children: [
    // 通过meta对象设置路由展示方式
    // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
    // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
    // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
    { path: '/home', component: _import('common/home'), name: 'home', meta: { /* title: '首页' */ } },
    { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
    // 产品详情页
    { path: '/product/:id', component: _import('modules/product/detail'), name: 'product-detail', meta: { title: '产品详情', isTab: true } }
    // 【ota 升级】升级记录 / 测试记录
    //{ path: '/ota/:id', component: _import('modules/ota/upgrade/history'), name: 'ota-history', meta: { title: 'ota记录', isTab: true }, props: true },
    // 【ota 升级】单个升级详情 / 单个测试详情
    //{ path: '/ota/:otaId/job/:type/:id', component: _import('modules/ota/upgrade/job'), name: 'ota-job', meta: { title: 'ota任务详情', isTab: true }, props: true },
    // 【ota 升级】版本历史
    //{ path: '/ota/:id/version-history', component: _import('modules/ota/upgrade/version-history'), name: 'ota-version-history', meta: { title: 'ota版本历史', isTab: true }, props: true }
  ],
  beforeEnter (to, from, next) {
    const token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      clearLoginInfo()
      next({ name: 'login' })
    }
    next()
  }
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  routes: globalRoutes.concat(mainRoutes)
})

const defaultPageTitle = 'TcloudWeb Admin'

router.beforeEach(async (to, from, next) => {
  document.title = to.meta.title ? `${to.meta.title} - ${defaultPageTitle}` : defaultPageTitle

  // 添加动态(菜单)路由
  // 1. 已经添加 or 全局路由, 直接访问
  // 2. 获取菜单列表, 添加并保存本地存储
  if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
    next()
  } else {
    const { data } = await http({
      url: http.adornUrl('/sys/menu/nav'),
      method: 'get',
      params: http.adornParams()
    }).catch((e) => {
      console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
      router.push({ name: 'login' })
    })
    if (data && data.code === 200) {
      fnAddDynamicMenuRoutes(data.menuList)
      router.options.isAddDynamicMenuRoutes = true
      // sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
      // sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
      store.commit('user/updateMenu', data.menuList)
      store.commit('user/updatePermissions', data.permissions)
      next({ ...to, replace: true })
    } else {
      // sessionStorage.setItem('menuList', '[]')
      // sessionStorage.setItem('permissions', '[]')
      store.commit('user/updateMenu', [])
      store.commit('user/updatePermissions', [])
      next()
    }
    // catch((e) => {
    //   console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
    //   router.push({ name: 'login' })
    // })
  }
})

/**
 * 判断当前路由类型, global: 全局路由, main: 主入口路由
 * @param {*} route 当前路由
 * @param globalRoutes
 */
function fnCurrentRouteType (route, globalRoutes = []) {
  let temp = []
  for (let i = 0; i < globalRoutes.length; i++) {
    if (route.path === globalRoutes[i].path) {
      return 'global'
    } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
      temp = temp.concat(globalRoutes[i].children)
    }
  }
  return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
}

/**
 * 添加动态(菜单)路由
 * @param {*} menuList 菜单列表
 * @param {*} routes 递归创建的动态(菜单)路由
 */
function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  let temp = []
  for (let i = 0; i < menuList.length; i++) {
    if (menuList[i].list && menuList[i].list.length >= 1) {
      temp = temp.concat(menuList[i].list)
    } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
      menuList[i].url = menuList[i].url.replace(/^\//, '')
      const route = {
        path: menuList[i].url.replace('/', '-'),
        component: null,
        name: menuList[i].url.replace('/', '-'),
        meta: {
          menuId: menuList[i].menuId,
          title: menuList[i].name,
          isDynamic: true,
          isTab: true,
          iframeUrl: ''
        }
      }
      // url以http[s]://开头, 通过iframe展示
      if (isURL(menuList[i].url)) {
        route.path = `i-${menuList[i].menuId}`
        route.name = `i-${menuList[i].menuId}`
        route.meta.iframeUrl = menuList[i].url
      } else {
        try {
          route.component = _import(`modules/${menuList[i].url}`) || null
        } catch (e) {}
      }
      routes.push(route)
    }
  }
  if (temp.length >= 1) {
    fnAddDynamicMenuRoutes(temp, routes)
  } else {
    mainRoutes.name = 'main-dynamic'
    mainRoutes.children = routes
    router.addRoutes([
      mainRoutes,
      { path: '*', redirect: { name: '404' } }
    ])
    sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
    console.log('\n')
    console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
    console.log(mainRoutes.children)
    console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  }
}

export default router

Enter fullscreen mode Exit fullscreen mode

Top comments (0)