当前位置: 首页 > news >正文

Vue2 动态路由

VUE CLI 项目

router.js

import Vue from "vue";
import Router from "vue-router";
import base from "@/view/404/404.vue";const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {return originalPush.call(this, location).catch(err => err)
}Vue.use(Router);//配置路由
const router = new Router({// mode: "history",mode: "hash",routes: [{path: "/",name: "Login",component: () => import("@/view/login.vue"),},{path: "/Layout",name: "Layout",component: () => import("@/view/layout.vue")},{path: '/404',name: 'base',component: base}],
});//导出
export default router;

main.js


var getRouter //用来获取后台拿到的路由
router.beforeEach((to, from, next) => {if (!getRouter) { //不加这个判断,路由会陷入死循环if (to.path == '/' || to.path == '/Layout') {if(to.path == "/"){Vue.prototype.nativeplacestore.clear()}next()} else {if (!Vue.prototype.nativeplacestore.get("uu")) {next()} else {getRouter = Vue.prototype.nativeplacestore.get("uu")getRouter.map(x=>{router.addRoute("Layout",{path: "/Layout/"+x.path,name: "Path"+x.path,component: () => import("@/view/1.vue"),children:[]})return {path: "/Layout/"+x.path,name: "Path"+x.path,component: () => import("@/view/1.vue"),}})if(Vue.prototype.nativeplacestore.get("WenShiDu")){router.addRoute("Layout",{path: "/Layout/WenShiDu",name: "WenShiDu",component: () => import("@/view/2.vue"),children:[]})}router.addRoute({path: '*',redirect: "/404",})next({...to,replace: true})}}} else {next()}
})
http://www.lryc.cn/news/348156.html

相关文章:

  • LeetCode746:使用最小花费爬楼梯
  • DockerFile介绍与使用
  • Java基础知识(六) 字符串
  • 为什么跨境电商大佬都在自养号测评?看完你就懂了!
  • AtCoder Beginner Contest 353
  • 深度解读《深度探索C++对象模型》之虚继承的实现分析和效率评测(一)
  • 计算机Java项目|Springboot房产销售系统
  • 学习3D几何和特征一致的高斯溅射目标去除
  • PHP 使用常量实现枚举类
  • Linux操作系统基础题库
  • Java抽象类:为何它是你代码架构的基石?
  • Flutter 中的 ToggleButtons 小部件:全面指南
  • 【MYSQL】一颗B+树可以保存多少条数据
  • ssm125四六级报名与成绩查询系统+jsp
  • 【Unity从零开始学习制作手机游戏】第01节:控制3D胶囊体运动
  • 内容安全(DPI和DFI解析)
  • 2024数维杯数学建模A题B题C题思路+模型+代码(开赛后第一时间更新)
  • SpringSecurity多表,多端账户登录
  • 绝地求生PUBG新老艾伦格有什么差别 老艾伦格什么时候回归
  • Windows下安装Node.js、npm和electronic,并运行一个Hello, World!脚本程序
  • 【精品案例】化工炼化企业信息化建设解决方案(74页PPT)
  • 【Unity Animation 2D】Unity Animation 2D骨骼绑定与动画制作
  • 工器具管理(基于若依)
  • UE4_照亮环境_光束light beam
  • springboot3项目练习详细步骤(第三部分:文章管理模块)
  • 【面试八股总结】C++11新特性:智能指针
  • 【教程向】从零开始创建浏览器插件(二)深入理解 Chrome 扩展的 manifest.json 配置文件
  • 美易官方:美国房地产贷款逾期率飙升,银行业危机仍可控?现货黄金暂守2360
  • SwiftUI中的@StateObject和@ObservedObject的区别
  • 类与对象(二)