3.19 蓝桥杯+vue

本文详细介绍了在Vue项目中使用路由时,如何处理query参数、遇到torefs限制的情况,以及如何进行编程式导航和重定向。内容涵盖了`vue-router`组件的props和params配置,以及使用`createWebHistory`创建历史API。
摘要由CSDN通过智能技术生成

vue

路由

query传参

但是torefs好像用不了

<template>
  <ul class="news_list"></ul>
  <li>编号:{{ route.query.id }}</li>
  <li>标题:{{ route.query.title }}</li>
  <li>内容:{{ route.query.content }}</li>
</template>

<script setup>
import { useRoute } from 'vue-router';
//import { toRefs} from 'vue';
  let route = useRoute()
  //let {query} = toRefs(route)
</script>

<style>

</style>
<template>
<div class="news">
    <ul>
        <li v-for="news in newsList" :key="news.id">
            <!-- <RouterLink to="/news/detail">{{ news.title }}</RouterLink> -->

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

                    {{ news.title }}
            </RouterLink>
        </li>

    </ul>
</div>

<div class="news_content">
    <RouterView></RouterView>
</div>
</template>

<script setup>
import { reactive } from 'vue';
import { RouterView,RouterLink } from 'vue-router';
const newsList = reactive([
    { id: 'aa1', title: '好', content: 'A' },
    { id: 'aa2', title: '不好', content: 'B' },
    { id: 'aa3', title: '很好', content: 'C' },
])
</script>

params

● 规则需要占位 path: ‘detail/:id/:title/:content’
● 路由不能用path,那就只能用name
● 规则可传可不传,用?path: ‘detail/:id/:title/:content?’

<RouterLink
    :to="{
    name:'ziluyou',
    params: {
        id: news.id,
        title: news.title,
        content: news.content
                    }
                }">

        {{ news.title }}
</RouterLink>

props配置

第一种写法:只能处理params参数

        path: '/news',
        component: news,
        children: [{
            name: 'ziluyou',
            path: 'detail/:id/:title/:content',
            component: detail,
            props: true
        }]
<template>
  <ul class="news_list"></ul>
  <li>编号:{{ id }}</li>
  <li>标题:{{ title }}</li>
  <li>内容:{{ content }}</li>
</template>

<script setup>
  defineProps(['id','title','content'])
</script>

第二种写法(query可用

children: [{
    name: 'ziluyou',
    path: 'detail',
    component: detail,
    //props: true
    props(route) {
        return route.query
    }
}]

编程式路由导航

router里面写的和上面的to里面一模一样

<button @click="showNewDetail(news)">查看新闻</button>



const router = useRouter()

function showNewDetail(news) {
    router.push({
        name:'ziluyou',
        query: {
            id: news.id,
            title: news.title,
            content: news.content
        }
    })
}

重定向

const router = createRouter({
    history: createWebHistory(),
    routes: [{
        path: '/about',
        component: about
    }, {
        path: '/person',
        component: person
    }, {
        path: '/',
        redirect: '/about'
    }]
})
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

o_o O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值