• 从Vue 2到Vue 3:深入了解路由配置的变化与升级建议


    在这里插入图片描述

    🎬 岸边的风个人主页

     🔥 个人专栏:《 VUE 》 《 javaScript 》

    ⛺️生活的理想,就是为了理想的生活!

    目录

    📘 前言

    vue2路由配置

    📟 一、控制台安装vue路由

    📟 二、项目src文件夹下创建router文件夹,并在router文件夹下创建index.js文件

     📟 三、在index.js文件夹下进行vue路由配置

    📟 四、在main.js中注册路由

    📟 五、在App.vue根组件组件使用

    📟 六、后记

    📘 vue3路由配置

    📟 一、控制台安装vue路由

    📟 二、项目src文件夹下创建router文件夹,并在router文件夹下创建index.tshe==文件

     📟 三、在index.js文件夹下进行vue路由配置

    📟 四、在main.js中注册路由

    📟 五、在App.vue根组件组件使用

    📟 六、后记

    📘 写在最后


    📘 前言

    欢迎阅读本篇文章,我们将带您深入探索Vue 2和Vue 3的路由配置。在现代前端开发中,路由是构建交互式Web应用程序不可或缺的一部分。Vue.js作为一种流行的JavaScript框架,在版本2和版本3之间进行了重大改进和升级。

    在这篇文章中,我们将比较Vue 2和Vue 3的路由配置,并介绍它们之间的主要区别和新特性。我们将探讨Vue Router的使用方法,包括路由的定义、嵌套路由的设置、路由守卫的应用等。我们还将深入研究Vue 3中的新特性,例如Composition API如何影响路由配置的方式。

    无论您是Vue 2的老手,还是想要了解Vue 3的新功能,本文都会为您提供全面和实用的指导。我们还将分享一些迁移Vue 2到Vue 3的实践经验和建议,帮助您平稳地过渡并兼顾项目的成功

    无论您是正在构建新的Vue应用程序,还是正在考虑将现有的Vue 2项目升级到Vue 3,本文都将为您提供有价值的信息和策略。让我们一起深入研究Vue 2和Vue 3的路由配置,为您的下一个Vue项目增添动力和灵活性

    vue2路由配置

    📟 一、控制台安装vue路由

    npm install --save vue-router@3.5.3 

    最新版本只支持vue3,所以vue2要安装3.5.3的版本

    📟 二、项目src文件夹下创建router文件夹,并在router文件夹下创建index.js文件

     📟 三、在index.js文件夹下进行vue路由配置

    1. import Vue from 'vue';
    2. import VueRouter from 'vue-router';
    3. // 使用VueRouter插件
    4. Vue.use(VueRouter);
    5. // 把VueRouter原型对象push,保存一份
    6. let originPush = VueRouter.prototype.push
    7. let originReplace = VueRouter.prototype.replace
    8. // 重写push|replace
    9. // 第一个参数:告诉原来的push方法,往哪里跳转(传递哪些参数)
    10. VueRouter.prototype.push = function (location, resolve, reject) {
    11. if (resolve && reject) {
    12. originPush.call(this, location, resolve, reject)
    13. } else {
    14. originPush.call(this, location, () => { }, () => { })
    15. }
    16. }
    17. VueRouter.prototype.replace = function (location, resolve, reject) {
    18. if (resolve && reject) {
    19. originReplace.call(this, location, resolve, reject)
    20. } else {
    21. originReplace.call(this, location, () => { }, () => { })
    22. }
    23. }
    24. // 创建路由对象
    25. const router = new VueRouter({
    26. mode: 'history',
    27. routes:[
    28. {
    29. path: "/",
    30. name: "Login",
    31. component: () => import("@/view/Login/index.vue"),
    32. meta:{
    33. show:true,
    34. title: "登陆页",
    35. menuOrder: 1,
    36. icon: "Remove"
    37. }
    38. },
    39. {
    40. path: "/home",
    41. name: "Home",
    42. component: () => import("../view/Home/index.vue"),
    43. children:[
    44. {
    45. path: "/Home-One",
    46. name: "Home-One",
    47. component: () => import("@/view/Home/One/index.vue"),
    48. meta:{
    49. show:true,
    50. title: "one页面",
    51. menuOrder: 1,
    52. icon: "el-icon-user-solid"
    53. }
    54. },
    55. ],
    56. meta:{
    57. show:true,
    58. title: "hom页面",
    59. menuOrder: 1,
    60. icon: "el-icon-s-tools"
    61. }
    62. },
    63. {
    64. path: "/about",
    65. name: "About",
    66. component: () => import("@/view/About/index.vue"),
    67. meta:{
    68. show:true,
    69. title: "关于页面",
    70. menuOrder: 1,
    71. icon: "el-icon-menu"
    72. }
    73. },
    74. ]
    75. });
    76. export default router;

    📟 四、在main.js中注册路由

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. import router from '@/router/index';
    4. Vue.config.productionTip = false
    5. new Vue({
    6. router, //注册路由
    7. render: h => h(App),
    8. }).$mount('#app')

    📟 五、在App.vue根组件组件使用

    1. <script>
    2. export default {
    3. name: 'App',
    4. components: {
    5. }
    6. }
    7. script>
    8. <style>
    9. style>

    📟 六、后记

    本节讲述了vue2中路由的基本使用,后续如在项目中遇到问题可以私信我共同交流学习!

    📘 vue3路由配置

    📟 一、控制台安装vue路由

    npm install --save vue-router

    📟 二、项目src文件夹下创建router文件夹,并在router文件夹下创建index.tshe==文件

     📟 三、在index.js文件夹下进行vue路由配置

    1. import { createRouter, createWebHistory ,RouteRecordRaw} from 'vue-router'
    2. import {routes} from './router'
    3. const router = createRouter({
    4. history: createWebHistory(), //模式配置 hash模式
    5. routes:routes as RouteRecordRaw[]
    6. })
    7. console.log("--routes-->", routes);
    8. export default router

    📟 四、在main.js中注册路由

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. import router from '@/router/index';
    4. Vue.config.productionTip = false
    5. new Vue({
    6. router, //注册路由
    7. render: h => h(App),
    8. }).$mount('#app')

    📟 五、在App.vue根组件组件使用

    1. <script>
    2. export default {
    3. name: 'App',
    4. components: {
    5. }
    6. }
    7. script>
    8. <style>
    9. style>

    📟 六、后记

    vue3的配置与vue2是有所差别的,本文就讲述了如何配置,如果本文对你有所帮助请三连支持博主。


    📘 写在最后

    无论是Vue 2还是Vue 3,路由配置都需要细致的规划和设计。合理的路由结构、嵌套路由的使用以及路由守卫的应用都是关键因素。同时,了解Vue Router的特性和用法,能够更好地利用路由实现页面导航、状态管理等功能。

    感谢大家一如既往对我的点赞与支持,不是我的作品有多好,而是你们的不嫌弃。这世界上有一种爱叫“关注”,感恩遇见、感恩有你~

     

     

  • 相关阅读:
    阿里云部署应用
    try/catch/finally的各种情况
    1.Spring Cloud Eureka 简介
    UltraEdit2024免费版文本编辑器
    【LeetCode】Day104-无重叠区间
    基于JAVA医院住院部管理计算机毕业设计源码+系统+数据库+lw文档+部署
    Redis集群服务器
    Netty使用
    用户代理字符串检测技术【1】
    我的2023年度关键词:ChatGPT、生产力工具
  • 原文地址:https://blog.csdn.net/qq_48652579/article/details/131984992