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>
相关推荐
海鸥两三1 小时前
uniapp 小程序引入 uview plus 框架,获得精美的UI框架
前端·vue.js·ui·小程序·uni-app
lightgis2 小时前
16openlayers加载COG(云优化Geotiff)
前端·javascript·html·html5
小飞大王6662 小时前
TypeScript核心类型系统完全指南
前端·javascript·typescript
你的人类朋友4 小时前
✍️记录自己的git分支管理实践
前端·git·后端
合作小小程序员小小店5 小时前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
防火墙在线5 小时前
前后端通信加解密(Web Crypto API )
前端·vue.js·网络协议·node.js·express
Jacky-0085 小时前
Node + vite + React 创建项目
前端·react.js·前端框架
CoderYanger6 小时前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_6 小时前
10个css更新
前端·css
她是太阳,好耀眼i6 小时前
Nvm 实现vue版本切换
javascript·vue.js·ecmascript