• vue路由


    一、什么是Vue路由

    Vue.js 路由允许我们通过不同的 URL 访问不同的内容。

    通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA)。

    二、路由配置 /router/index

    1、普通路由配置

    1. {
    2. path: '/user', //配置的路径
    3. name: 'user', //名称
    4. component: () => import('xxx)
    5. }

    2、路由传参

    1. {
    2. path:“/product/:id”,
    3. name:"product",
    4. component:xxx
    5. }
    6. "/product/abc">
    7. 在页面中获取:$route.params.id
    8. }

    三、404页面

    1. // 404的path为* 配置在最后面
    2. {
    3. path: "*",
    4. name: "nomatch",
    5. component: () => import('../views/NoMatch.vue')
    6. }

    四、编程式路由跳转

    1. 前进   forward()    go(1)
    2. 后退   go(-1)    back()
    3. 切换路由    push("/about")
    4. 替换路由    replace("/about")

    三、路由信息

    • 路由参数 params
    • 查询 query
    • 地址 path
    • 全地址 fullPath
    • 名称 name
    1. <template>
    2. <div>
    3. <h1>产品页面{{$route.params.id}}h1>
    4. <p>查询参数:{{$route.query}}p>
    5. <p>哈希值:{{$route.hash}}p>
    6. <p>全地址:{{$route.fullPath}}p>
    7. <p>地址:{{$route.path}}p>
    8. <p>名称:{{$route.name}}p>
    9. <button @click='$router.back()'>返回button>
    10. div>
    11. <template>

    四、路由守卫

    (1)组件内部

              进入前:beforeRouterEnter(to,from,next)

                            to 要进入的路由

                            from 从哪个页面进入

                            next 下一步  

                            next()进入to页面

                            next(true)进入to页面

                            next(false)不跳转

                            next(“/login”)跳转到登录

                离开前 :beforeRouterLeave

                更新前:beforeRouterUpdate

    (2)全局
            beforeEach((to,from,next)=>{})

            afterEach

    (3) 独享

            beforeEnter()

    五、路由参数查询

    传递
    next("/login?redirect=/cart")

    获取
    var redirect = this.$route.query.redirect||'/user'

  • 相关阅读:
    【墨染】找特有姿态!基于【灵茶山艾府】题解的补充图解
    C++精通之路:红黑树
    linux基本指令(下)
    【算法基础】双指针
    pytorch的使用:卷积神经网络模块
    transformer 最简单学习3, 训练文本数据输入的形式
    MySQL第十二讲:ShardingJDBC详解
    新加坡科技巨头Sea亏损小于预期,外资“清算”阿里只为加大赌注
    国内大型语言模型(LLM)的研发及突破性应用
    机器学习__02__机器学习工程实践
  • 原文地址:https://blog.csdn.net/m0_67316947/article/details/126198556