当前位置:   article > 正文

vue 动态拼接路由 完整简洁_动态拼接路由component import

动态拼接路由component import
import Vue from "vue";
import Router from "vue-router";
import Home from "@/pages/Home/home.vue";
import jgcx from "@/pages/jgcx/jgcx.vue";
import zxts from "@/pages/zxts/zxts.vue";
import notice from "@/pages/notice/notice.vue";
import noticeList from "@/pages/notice/noticeList.vue";
import noticeDetails from "@/pages/notice/noticeDetails.vue";
import iframe from "@/pages/iframe/iframe.vue";
// 获取导航数据
import { getnavigation } from '@/api/apiList.js'

import { store } from "@/store/store.js";


const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(Router);
const routes = [
  {
    name: "iframe",
    path: "/iframe",
    component: iframe,
  },
]
// 实例化 Router 对象
const router = new Router({
  // linkActiveClass:'selected',
  routes
  // mode: 'history'
})
// 路由拦截 在跳转之前
router.beforeEach((to, from, next) => {
  addData()
  next()
})
function addData() {
  // 请求接口 路由数据
  getnavigation().then(res => {
    if (res.code == "200") {
      let navData = res.data.records
      let eventPath = navData[0].jumpUrl.split('#')[1].split('?')[0]
      sessionStorage.setItem("eventPath",eventPath)
      // 拼接路由 根据自己获取到的数据灵活应用
      navData.forEach((item, index) => {
        let url2 = item.jumpUrl

        if (url2.split('#')[1]) {
          let path = url2.split('#')[1].split('?')[0]
          router.addRoute({
            path: path,
            name: path,
            component: () => import('../pages/notice' + path)
          })
        }
      })

    }
  })
}

export default router
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/59008
推荐阅读
相关标签
  

闽ICP备14008679号