0
点赞
收藏
分享

微信扫一扫

vue3 路由嵌套/动态路由/编程式路由

1.路由嵌套

实际生活中的应用界面,通常由多层嵌套的组件组合而成。
一个页面当中有多个子页面 这种结构叫做嵌套路由。

比如需要在/about路由下嵌套三个子路由:

需要在children中指定相应的子路由

// /about路由
{
    path: '/about',
    redirect:'/base',
    name:'About',
    component:()=>import("../components/About.vue"),
    meta: {
      keepAlive: true,
      title:'about'
    },
    children: [
      {
        path: '/base',
        name:'base',
        component:()=>import("../components/base.vue")
      },
       {
        path: '/photo',
        name:'photo',
        component:()=>import("../components/photo.vue")
      },
        {
        path: '/more',
        name:'more',
        component:()=>import("../components/more.vue")
      }
    ]
  },

  //about.vue组件中配置
  <router-link to="/base">基本信息组件</router-link> |
    <router-link to="/photo">头像照片组件</router-link> |
    <router-link to="/more">跟下更多个人信息组件</router-link>
  <router-view></router-view>

为了使子路由更具有语义,我们可以在子路由上加上父级路由地址:

//  /about路由
{
    path: '/about',
    redirect:'/about/base',
    name:'About',
    component:()=>import("../components/About.vue"),
    meta: {
      keepAlive: true,
      title:'about'
    },
    children: [
      {
        path: '/about/base',
        name:'base',
        component:()=>import("../components/base.vue")
      },
       {
        path: '/about/photo',
        name:'photo',
        component:()=>import("../components/photo.vue")
      },
        {
        path: '/about/more',
        name:'more',
        component:()=>import("../components/more.vue")
      }
    ]
  },

  //about.vue组件
  <router-link to="/about/base">基本信息组件</router-link> |
    <router-link to="/about/photo">头像照片组件</router-link> |
    <router-link to="/about/more">跟下更多个人信息组件</router-link>
   <router-view></router-view>

1.动态路由

我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件。例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染。那么,我们可以在 vue-router 的路由路径中使用“动态路径参数”(dynamic segment) 来达到这个效果:我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件。例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染。那么,我们可以在 vue-router 的路由路径中使用“动态路径参数”(dynamic segment) 来达到这个效果:

1.params传参

可以直接在链接上传递参数

<router-link to="/home/lucky">home</router-link>

在当前的路由需要指定一下传递的属性名字

{
    //对应的路径
    //指定name属性
    path: '/home/:name',
    //路由对应的名字
    name:'Home',
    //对应的组件
    component:()=>import("../components/Home.vue"),
    meta: {
      keepAlive: true,
      title:'home'
    }
  },

获取传递的属性值:

<p>params传参:{{$route.params.name}}</p>

还可以传递多个参数:

//router-link链接

 <router-link to="/home/lucky/1003015">home</router-link>

 //相关路由
 {
    //对应的路径
    path: '/home/:name/:id',
    //路由对应的名字
    name:'Home',
    //对应的组件
    component:()=>import("../components/Home.vue"),
    meta: {
      keepAlive: true,
      title:'home'
    }
  }

  //home.vue组件
  <p>params传参:name:{{$route.params.name}},id:{{$route.params.id}}</p>

2.query传参

//router-link链接

<router-link to="/about?title=文章列表&id=100813">about</router-link>
//相关路由不需要配置

//about.vue组件
<p>query传参,title:{{$route.query.title}},id:{{$route.query.id}}</p>

1.编程式的路由

编程式的导航 除了使用 创建 a 标签来定义导航链接 我们还可以用js的方法跳转页面

所谓的编程式路由,就是通过代码来实现路由。
和前面的router-link进行对比。router-link是通过点击来进行跳转的。而编程式路由,就没有router-link,而是通过代码直接跳转。

1.UI组件的跳转方式

<table cellspacing="0" cellpadding="0" border="1">
      <tr>
        <th>电影名字</th>
        <th>上映时间</th>
        <th>电影时长</th>
        <th>电影海报</th>
      </tr>
      <tr v-for="item in movieList" :key="item.mId">
        <td>{{item.title}}</td>
        <td>{{item.year}}</td>
        <td>{{item.longtime}}</td>

  <!-- ui的方式通过path跳转 -->
   <!-- ui的方式通过path跳转 -->
     <!-- <td>
        <router-link :to="{path:'/detail',query:{id:item.mId}}">
          <img :src="item.small" width="100" height="110">
        </router-link>
        </td> -->
  <!-- ui的方式通过name跳转 -->
     <td>
        <router-link :to="{name:'movieDetail',query:{id:item.mId}}">
          <img :src="item.small" width="100" height="110">
        </router-link>
      </td>
      </tr>
    </table>

2.路由代码跳转

<template>
  <div>
    <h3>电影列表页</h3>
    <table cellspacing="0" cellpadding="0" border="1">
      <tr>
        <th>电影名字</th>
        <th>上映时间</th>
        <th>电影时长</th>
        <th>电影海报</th>
      </tr>
      <tr v-for="item in movieList" :key="item.mId">
        <td>{{item.title}}</td>
        <td>{{item.year}}</td>
        <td>{{item.longtime}}</td>
     <td @click="toDetail(item)">
          <img :src="item.small" width="100" height="110">
      </td>
      </tr>
    </table>

  </div>
</template>
<script setup>
import axios from "axios"
import { onMounted,reactive,ref} from "vue";
//引入跳转路由
import {useRouter} from "vue-router"
 var  movieList=ref([])

 onMounted(()=>{
  axios.get("http://bufantec.com/api/douban/movie/in_theaters")
  .then(res=>{
    console.log(res);
    const {data}=res.data;
    movieList.value=data.list;
  })
 })
 //跳转到详情页
 const router=useRouter();
 const toDetail=(item)=>{
    //路由跳转和传递参数
    //1.path 和query组合
    // router.push({path:'/detail',query:{id:item.mId}})
    //2.path 和params组合 不能一起使用
    //router.push({path:'/detail',params:{id:item.mId}})
    //3.name和query组合
    //router.push({name:'movieDetail',query:{id:item.mId}})
    //4.name和params 组合
      router.push({name:'movieDetail',params:{id:item.mId}})
 }
</script>
<style lang="scss">
table{
  width: 500px;
  margin: 0 auto;
  td{
    text-align: center;
  }
}
</style>

3.useRoute()和 useRouter()的区别

这两个方法类似于$route 和 $router的区别。

useRoute():

vue3  路由嵌套/动态路由/编程式路由_ios

route是一个跳转的路由对象,每一个路由都会有一个route对象,是一个局部的对象,可以获取对应的name,path,params,query等:

matched:
数组,包含当前匹配的路径中所包含的所有片段所对应的配置参数对象
name:
当前路径的名字,如果没有使用具名路径,则名字为空。
params:
对象,包含路由中的动态片段和全匹配片段的键值对
path:
字符串,等于当前路由对象的路径,会被解析为绝对路径,如 “/home/news”
query:
对象,包含路由中查询参数的键值对。例如,对于 /home/news/detail/01?favorite=yes ,会得到$route.query.favorite == ‘yes’
redirectedFrom

useRouter():

vue3  路由嵌套/动态路由/编程式路由_ci_02

router是VueRouter的一个对象,通过Vue.use(VueRouter)和VueRouter构造函数得到一个router的实例对象,这个对象中是一个全局的对象,他包含了所有的路由包含了许多关键的对象和属性。

举例:history对象

$router.push({path:‘home’});本质是向history栈中添加一个路由,在我们看来是 切换路由,但本质是在添加一个history记录

方法:
$router.replace({path:‘home’});//替换路由,没有历史记录

举报

相关推荐

vue3动态路由addRoute

0 条评论