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>
相关推荐
学渣y8 分钟前
React状态管理-对state进行保留和重置
javascript·react.js·ecmascript
_龙衣27 分钟前
将 swagger 接口导入 apifox 查看及调试
前端·javascript·css·vue.js·css3
进取星辰1 小时前
25、Tailwind:魔法速记术——React 19 样式新思路
前端·react.js·前端框架
struggle20252 小时前
continue通过我们的开源 IDE 扩展和模型、规则、提示、文档和其他构建块中心,创建、共享和使用自定义 AI 代码助手
javascript·ide·python·typescript·开源
x-cmd2 小时前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
前端·javascript·windows·npm·node.js
夏之小星星2 小时前
el-tree结合checkbox实现数据回显
前端·javascript·vue.js
crazyme_63 小时前
前端自学入门:HTML 基础详解与学习路线指引
前端·学习·html
撸猫7913 小时前
HttpSession 的运行原理
前端·后端·cookie·httpsession
亦世凡华、3 小时前
Rollup入门与进阶:为现代Web应用构建超小的打包文件
前端·经验分享·rollup·配置项目·前端分享
琉璃℡初雪3 小时前
vue2/3 中使用 @vue-office/docx 在网页中预览(docx、excel、pdf)文件
vue.js·pdf·excel