Vue2进阶篇-配置路由、路由传参与路由路径的两种模式

Vue2基础全套教程合集:点击跳转        Vue2高级全套教程合集:点击跳转


一、路由的基本使用

  1. 安装vue-router,命令:npm i vue-router

  2. 应用插件:Vue.use(VueRouter)

  3. 编写router配置项:

    //引入VueRouter
    import VueRouter from 'vue-router'
    //引入Luyou 组件
    import About from '../components/About'
    import Home from '../components/Home'
    
    //创建router实例对象,去管理一组一组的路由规则
    const router = new VueRouter({
        routes:[
            {
                path:'/about',
                component:About
            },
            {
                path:'/home',
                component:Home
            }
        ]
    })
    
    //暴露router
    export default router
    
  4. 实现切换(active-class可配置高亮样式)

    <router-link active-class="active" to="/about">About</router-link>
    
  5. 指定展示位置

    <router-view></router-view>
    

二、多级路由

  1. 配置路由规则,使用children配置项:

    routes:[
        {
            path:'/about',
            component:About,
        },
        {
            path:'/home',
            component:Home,
            children:[ //通过children配置子级路由
                {
                    path:'news', //此处一定不要写:/news
                    component:News
                },
                {
                    path:'message',//此处一定不要写:/message
                    component:Message
                }
            ]
        }
    ]
    
  2. 跳转(要写完整路径):

    <router-link to="/home/news">News</router-link>
    

三、命名路由

  1. 作用:可以简化路由的跳转。

  2. 如何使用

    1. 给路由命名:

      {
          path:'/demo',
          component:Demo,
          children:[
              {
                  path:'test',
                  component:Test,
                  children:[
                      {
                         	name:'hello' //给路由命名
                          path:'welcome',
                          component:Hello,
                      }
                  ]
              }
          ]
      }
      
    2. 简化跳转:

      <!--简化前,需要写完整的路径 -->
      <router-link to="/demo/test/welcome">跳转</router-link>
      
      <!--简化后,直接通过名字跳转 -->
      <router-link :to="{name:'hello'}">跳转</router-link>
      
      <!--简化写法配合传递参数 -->
      <router-link 
          :to="{
              name:'hello',
              query:{
                 id:666,
                  title:'你好'
              }
          }"
      >跳转</router-link>
      

四、路由传参

1. query传参

  1. 传递参数

    <!-- 跳转并携带query参数,to的字符串写法 -->
    <router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link>
    
    <!-- 跳转并携带query参数,to的对象写法 -->
    <router-link 
        :to="{
            path:'/home/message/detail',
            query:{
               id:666,
                title:'你好'
            }
        }"
    >跳转</router-link>
    
  2. 接收参数:可以这样直接写入模板表达式中。

    this;$route.query.id
    this;$route.query.title
    

    也可以通过computed属性来简化模板表达式的书写。

    例:

     computed: {
     	id(){
     		return this.$route.query.id
     	},
     	title(){
     		return this.$route.query.title
     	},
     },
    

2. params传参

  1. 配置路由,声明接收params参数

    {
        path:'/home',
        component:Home,
        children:[
            {
                path:'news',
                component:News
            },
            {
                component:Message,
                children:[
                    {
                        name:'xiangqing',
                        path:'detail/:id/:title', //使用占位符声明接收params参数
                        component:Detail
                    }
                ]
            }
        ]
    }
    
  2. 传递参数

    <!-- 跳转并携带params参数,to的字符串写法 -->
    <router-link :to="/home/message/detail/666/你好">跳转</router-link>
    
    <!-- 跳转并携带params参数,to的对象写法 -->
    <router-link 
        :to="{
            name:'xiangqing',
            params:{
               id:666,
                title:'你好'
            }
        }"
    >跳转</router-link>
    

    特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!

  3. 接收参数:可以这样直接写入模板表达式中。

    this;$route.params.id
    this;$route.params.title
    

    也可以通过computed属性来简化模板表达式的书写。

    例:

     computed: {
     	id(){
     		return this.$route.params.id
     	},
     	title(){
     		return this.$route.params.title
     	},
     },
    

3. 传参问题

  1. 如何指定params参数可以传也可以不传?
    答:在path路径后的params占位符后加?,例如:path:'detail/:id?

  2. params参数设置可以传也可以不传,但如果传递空串怎么办?
    答:使用undefined解决。例如:params: {keyword: '' || undefined }

  3. 组件是否可以传递props数据
    答:可以,请看下面的路由props配置。

五、路由的props配置

​ 作用:让路由组件更方便的收到参数,组件方便了,路由配置麻烦了点(需要有取舍)。

{
    name:'xiangqing',
    path:'detail/:id',
    component:Detail,

    //第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
    // props:{a:900}

    //第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有 params参数 通过props传给Detail组件
    // props:true

    //第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
    props($route){
        return {
            id:$route.query.id,
            title:$route.query.title
        }
    }
}

组件接收:

<template>
	<ul>
		<li>消息编号:{{id}}</li>
		<li>消息标题:{{title}}</li>
	</ul>
</template>

<script>
	export default {
		name:'Detail',
		props:['id','title'],
	}
</script>

六、router-link的replace属性

  1. 作用: 控制路由跳转时操作浏览器历史记录的模式

  2. 浏览器的历史记录有两种写入方式:分别为pushreplacepush是追加历史记录,replace是替换当前记录。路由跳转时候默认为push

  3. 如何开启replace模式:<router-link replace .......>News</router-link>

    例:

    <template>
    	<div>
    		<h2>Home组件内容</h2>
    		<div>
    			<ul class="nav nav-tabs">
    				<li>
    					<router-link replace class="list-group-item" active-class="active" to="/home/news">News</router-link>
    				</li>
    				<li>
    					<router-link replace class="list-group-item" active-class="active" to="/home/message">Message</router-link>
    				</li>
    			</ul>
    			<router-view></router-view>
    		</div>
    	</div>
    </template>
    

    案例: 点击A路由 = => 点击B路由(replace模式) = => 点击C路由 = => 点击D路由 = => 点击B路由(replace模式)。(没写则为push模式)

    浏览器浏览历史记录为: B C B。 注意:浏览器历史记录是栈结构,第一个B为栈底,最后一个为栈顶。

源代码出处:尚硅谷Vue2.0+Vue3.0全套教程丨vuejs从入门到精通

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值