Vue2路由案例实战

本文详细介绍了Vue2中的路由配置,包括基本使用、高亮激活状态、精确匹配、嵌套路由、重定向、缓存组件、参数传递、props配置以及编程式导航的方法和示例。
摘要由CSDN通过智能技术生成

Vue2路由案例实战

1、 基本使用

        <!-- 1、路由标签 -->
            <router-link to="/foo">foo</router-link>
        <!-- 2、路由出口 -->
            <router-view></router-view>
        <!-- 3、配置路由 -->
        const router = new VueRouter({
            routes:[
                {
                    path:'/foo',
                    component:Foo
                }
            ]
        })
        <!-- 4、vue实例对象中书写路由 -->
        let vm = new Vue({
            el:"#app",
            data:{
            },
            router
        })

2、 高亮 “active” 与 精确匹配 “exact”

        a、linkActiveClass:'active'
        详细:router-link 切换时的类; 可以理解为:点谁谁自动变为这个类
        b、<router-link tag="li" to="/" exact>
                    <a>首页</a>
            </router-link>
        详细:tag="li"<router-link> 渲染为 li; exact 开启精确匹配(路径完全一致才能匹配)

3、 嵌套路由

    a、配置路由规则,使用children配置项
        routes: [
            {
                path: '/news',
                component: news,
                // 二级路由 配置 children
                children: [
                    {
                        // 写法一:路径写全
                        path: '/news/ty',
                        component:Ty
                    },
                    {
                        // 写法二:简写 不写‘/’
                        path: 'kj',
                        component:Kj
                    },
                ]
            }
        ]
    b、跳转(要写完整路径):
        <router-link tag='li' to='/news/ty'>
            <a>体育</a>
        </router-link>

4、 路由重定向 redirect

    routes: [
                {
                    // 重定向
                    redirect: '/news/ty',
                    path: '/news',
                    component: news
                }
            ]

5、 路由缓存组件 keep-alive

作用: 让不展示的路由组件保持挂载,不被销毁。

    <!-- 在切换路由时 会记录当前状态 -->
    <keep-alive>
        <router-view></router-view>
    </keep-alive>

6、 路由传参 params

    a、配置路由:声明接收params参数
        routes: [
            {
                redirect: '/news/ty',
                path: '/news',
                component: news,
                // 二级路由 配置 children
                children: [
                    {
                        path: '/news/ty',
                        component: Ty,
                        children: [
                            {
                                path: '/news/ty/data/:id',     // 使用占位符声明接收params参数
                                component: TyList,
                                // props 配置项
                                props:true
                            }
                        ]
                    }
                ]
            }
        ]

    b、传递参数
        <router-link :to='"/news/ty/data/"+item.id'>{{item.title}}</router-link>

    c、接收参数:
        $route.params.id
        $route.params.title

7、 路由配置 的 props配置项

作用:让路由组件更方便的收到参数

    a、配置
       routes: [
            {
                redirect: '/news/ty',
                path: '/news',
                component: news,
                children: [
                    {
                        path: '/news/ty',
                        component: Ty,
                        children: [
                            {
                                path: '/news/ty/data/:id',
                                component: TyList,
                                props:true      // props 配置项
                            }
                        ]
                    }
                ]
            }
        ]

    b、使用
        id
        title

8、 编程式路由导航

作用:不借助<router-link> 实现路由跳转,让路由跳转更加灵活

    a、$router的两个API
        this.$router.push('/news/kj/kjlist')     // push 保存浏览器记录
        this.$router.replace('/news/kj/kjlist')     // replace 不会保存

    b、动作
        this.$router.forward()      // 前进
        this.$router.back()         // 后退
        this.$router.go()           // 可前进可后退 根据的是里面的值 1-1
  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值