[vue3]路由传参 query & params

Detail.vue

<template>
    <li>{{ id }}</li>
    <li>{{ title }}</li>
    <li>{{ content }}</li>

</template>

<script setup lang="ts" name="Detail">
    defineProps(['id','title','content'])
</script>

query

News.vue

<template>
    <div class="news"> 
        <ul>
            <li v-for="news in newsList" :key="news.id">
                <RouterLink
                    :to="{
                        path:'/news/detail',
                        query:{ // 写你想传入的内容
                            id:news.id,
                            title:news.title,
                            content:news.content
                        }
                    }"
                >
                    {{ news.title }}
                </RouterLink>
            </li>
        </ul>

        <RouterView></RouterView>

    </div>
</template>

<script setup lang="ts" name="News">
    import {reactive} from 'vue'
    import {RouterLink} from 'vue-router'

    const newsList = reactive(
        [
            {id:'1', title:'title1', content:'content1'},
            {id:'2', title:'title2', content:'content2'},
            {id:'3', title:'title3', content:'content3'}
        ]
    )
</script>

index.ts

import { createRouter, createWebHistory } 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:createWebHistory(), // 路由器的工作模式
    routes:[ // 一个一个的路由规则
        {
            path:'/home',
            component:Home
        },
        {
            path:'/news',
            component:News,
            children:[
                {
                    path:'detail',
                    component:Detail,
                    props(route) {
                        return route.query
                    }
                }
            ]
        },
        {
            path:'/about',
            component:About
        }
    ]
})

export default router

Params

index.ts

占位

                    path:'detail/:id/:title/:content',

import { createRouter, createWebHistory } 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:createWebHistory(), // 路由器的工作模式
    routes:[ // 一个一个的路由规则
        {
            path:'/home',
            component:Home
        },
        {
            path:'/news',
            component:News,
            children:[
                {
                    name:'detail',
                    path:'detail/:id/:title/:content',
                    component:Detail,
                    props(route) {
                        return route.params
                    }
                }
            ]
        },
        {
            path:'/about',
            component:About
        }
    ]
})

export default router

news.vue

用name而不是path

                        name:'detail',

<template>
    <div class="news"> 
        <ul>
            <li v-for="news in newsList" :key="news.id">
                <RouterLink
                    :to="{
                        name:'detail',
                        params:{
                            id:news.id,
                            title:news.title,
                            content:news.content,
                        }
                    }"
                >
                    {{ news.title }}
                </RouterLink>

            </li>
        </ul>

        <RouterView></RouterView>

    </div>
</template>

<script setup lang="ts" name="News">
import {reactive} from 'vue'
    import {RouterLink} from 'vue-router'

    const newsList = reactive(
        [
            {id:'1', title:'title1', content:'content1'},
            {id:'2', title:'title2', content:'content2'},
            {id:'3', title:'title3', content:'content3'}
        ]
    )
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值