VUE之参数传递

1、嵌套路由

路由嵌套 children里面的path属性不加/杠,可以参考如下代码:

javascript 复制代码
>> router/index.ts

// 创建一个路由器,并暴露出去

// 第一步:引入createRouter
import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
// 引入一个个可能呈现组件
import Home from '@/pages/Home.vue'
import News from '@/pages/News.vue'
import About from '@/pages/About.vue'
import Detail from '@/pages/Detail.vue'
// 第二步:创建路由器
const router = createRouter({
    history:createWebHashHistory(), //路由器的工作模式
    routes:[  //一个个路由规则
        {
            path:'/home',
            component: Home
        },
        {
            path:'/news',
            component: News,
            children:[
              {
                name: 'xiangqi',
                path:'detail',
                component:Detail
              }
            ]
        },
        {
            path:'/about',
            component: About
        },
    ]
})

export default router

2、query参数

javascript 复制代码
>> pages/Detail.vue

<template>
    <ul class="news-list">
        <li>编号: {
  
  {query.id}}</li>
        <li>编号: {
  
  {query.title}}</li>
        <li>编号: {
  
  {query.content}}</li>
    </ul>
</template>

<script lang="ts" setup name="Detail">
 import { toRefs } from 'vue';
 import { useRoute } from 'vue-router';
 let route = useRoute()
 //从响应式解构,会丢失响应式
 let { query } = toRefs(route)
 console.log(query)

</script>

<style scoped>
  .news-list {
    list-style: none;
    padding-left: 20px;
  }
  .news-list>li{
    line-height:30px;
  }
</style>

2.1、方法一

javascript 复制代码
>> pages/News.vue

<RouterLink :to="`/news/detail?id=${news.id}&title=${news.title}&content=${news.content}`">{
  
  {news.title}}</RouterLink>

<script setup lang="ts" name="News">
import { reactive } from 'vue';
import { RouterView,RouterLink } from 'vue-router';
const newList = reactive([
  {id:'dasfadadadad01',title:'很好的抗癌食物',content:'西蓝花'},
  {id:'dasfadadadad02',title:'如何一夜暴富',content:'学IT'},
  {id:'dasfadadadad03',title:'震惊万万没想到',content:'明天周一'},
  {id:'dasfadadadad04',title:'好消息',content:'快过年了'},
  ])


</script>

2.2、方法二

javascript 复制代码
<RouterLink :to="{
            path:'/news/detail',
            query:{
               id:news.id,
               title:news.title,
               content:news.content
             }
            }">
              {
  
  { news.title }}
          </RouterLink>

2.3、方法三

javascript 复制代码
<RouterLink :to="{
            name:'xiangqi',
            query:{
               id:news.id,
               title:news.title,
               content:news.content
             }
            }">
              {
  
  { news.title }}
          </RouterLink>

3、params参数

javascript 复制代码
>> router/index.ts     
   {
            path:'/news',
            component: News,
            children:[
              {
                name: 'xiangqi',
                path:'detail/:id/:title/:content?', //用于params参数占位,加?号可传可不传
                // path:'detail',
                component:Detail
              }
            ]
        },
javascript 复制代码
<template>
    <div class="news">
      <!--导航区-->
       <ul>
         <li v-for="news in newList" :key="news.id">
          <!--第一种写法-->
          <!-- <RouterLink :to="`/news/detail/${news.id}/${news.title}/${news.content}`">{
  
  {news.title}}</RouterLink> -->
          <!--第二种写法-->
          <RouterLink 
            :to="{
               name:'xiangqi',
               params: {  //params不能传对象和数组
                 id:news.id,
                 title:news.title,
                 content:news.content
               }
            }">
                 {
  
  {news.title}}
          </RouterLink>
         
         </li>
       </ul>
       <!--展示区-->
       <div class="news-content">
        <RouterView/>
       </div>
    </div>
</template>

<script setup lang="ts" name="News">
import { reactive } from 'vue';
import { RouterView,RouterLink } from 'vue-router';
const newList = reactive([
  {id:'dasfadadadad01',title:'很好的抗癌食物',content:'西蓝花'},
  {id:'dasfadadadad02',title:'如何一夜暴富',content:'学IT'},
  {id:'dasfadadadad03',title:'震惊万万没想到',content:'明天周一'},
  {id:'dasfadadadad04',title:'好消息',content:'快过年了'},
  ])

</script>
相关推荐
IT派同学34 分钟前
TableWiz诞生记:一个被表格合并逼疯的程序员如何自救
前端·vue.js
西洼工作室3 小时前
CSS高效开发三大方向
前端·css
昔人'3 小时前
css`font-variant-numeric: tabular-nums` 用来控制数字的样式。
前端·css
铅笔侠_小龙虾3 小时前
动手实现简单Vue.js ,探索Vue原理
前端·javascript·vue.js
sniper_fandc5 小时前
Axios快速上手
vue.js·axios
哟哟耶耶5 小时前
Starting again-02
开发语言·前端·javascript
Apifox.5 小时前
Apifox 9 月更新| AI 生成接口测试用例、在线文档调试能力全面升级、内置更多 HTTP 状态码、支持将目录转换为模块
前端·人工智能·后端·http·ai·测试用例·postman
Kitasan Burakku5 小时前
Typescript return type
前端·javascript·typescript
叁佰万5 小时前
前端实战开发(一):从参数优化到布局通信的全流程解决方案
前端
笔尖的记忆5 小时前
js异步任务你都知道了吗?
前端·面试