Vue3路由query參數(shù)實例詳解
Vue3路由query參數(shù)
基礎(chǔ)概念
query其實我們在說to的對象寫法的時候已經(jīng)大致介紹過了,現(xiàn)在來仔細的講講;query參數(shù)是URL中?后面的部分,用于傳遞鍵值對數(shù)據(jù);
例如https://example.com/users?page=1&search=vue
實例展示
- 首先為我們之前的項目,聯(lián)系我們設(shè)計一下嵌套路由,先更新路由的配置文件;
{
path: '/Contact',
component: Contact,
name: 'contact',
children: [{
path: '',
component: ContactContent.vue
}]
}]- 之后我們直接寫子組件代碼
<!-- components/ContactContent.vue -->
<template>
<div class="contact-content">
<!-- 根據(jù) query.type 顯示不同內(nèi)容 -->
<div v-if="contentType === 'phone'" class="content-section">
<h3>?? 電話聯(lián)系</h3>
<div class="info-card">
<p><strong>客服熱線:</strong> 400-123-4567</p>
<p><strong>技術(shù)支持:</strong> 400-123-4568</p>
<p><strong>投訴建議:</strong> 400-123-4569</p>
</div>
<div class="tips">
<p>工作時間:周一至周五 9:00-18:00</p>
</div>
</div>
<div v-else-if="contentType === 'email'" class="content-section">
<h3>?? 郵箱聯(lián)系</h3>
<div class="info-card">
<p><strong>客服郵箱:</strong> service@example.com</p>
<p><strong>技術(shù)支持:</strong> tech@example.com</p>
<p><strong>合作咨詢:</strong> cooperate@example.com</p>
</div>
<div class="tips">
<p>我們會在24小時內(nèi)回復(fù)您的郵件</p>
</div>
</div>
<div v-else-if="contentType === 'address'" class="content-section">
<h3>?? 地址信息</h3>
<div class="info-card">
<p><strong>總部地址:</strong> 北京市朝陽區(qū)某某街道123號</p>
<p><strong>上海分公司:</strong> 上海市浦東新區(qū)某某路456號</p>
<p><strong>深圳辦事處:</strong> 深圳市南山區(qū)科技園789號</p>
</div>
<div class="tips">
<p>來訪前請?zhí)崆邦A(yù)約</p>
</div>
</div>
<div v-else-if="contentType === 'hours'" class="content-section">
<h3>? 工作時間</h3>
<div class="info-card">
<p><strong>客服中心:</strong> 周一至周日 8:00-22:00</p>
<p><strong>技術(shù)支持:</strong> 周一至周五 9:00-18:00</p>
<p><strong>線下門店:</strong> 周一至周日 10:00-21:00</p>
</div>
<div class="tips">
<p>節(jié)假日工作時間可能調(diào)整,請關(guān)注公告</p>
</div>
</div>
<div v-else class="content-section">
<h3>?? 歡迎聯(lián)系我們</h3>
<p>請選擇上方的聯(lián)系方式查看詳細信息</p>
</div>
</div>
</template>- 我們這里使用v-if來判斷頁面的內(nèi)容
<script setup>
import { useRoute } from 'vue-router'
import { computed } from 'vue'
const route = useRoute()
// 從 query 參數(shù)獲取內(nèi)容類型
const contentType = computed(() => {
return route.query.type || 'phone' // 默認顯示電話信息
})
</script>這里使用一個計算屬性來獲取當(dāng)前路由的query的屬性,默認情況下顯示電話信息
- 順便展示一下CSS的內(nèi)容
<style scoped>
.contact-content {
padding: 20px;
}
.content-section h3 {
color: #333;
margin-bottom: 20px;
font-size: 1.5em;
}
.info-card {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
margin-bottom: 15px;
border-left: 4px solid #007bff;
}
.info-card p {
margin: 10px 0;
font-size: 16px;
}
.tips {
background: #e7f3ff;
padding: 12px 15px;
border-radius: 5px;
font-size: 14px;
color: #666;
}
</style>現(xiàn)在看主路由的配置
<template>
<div class="contact-container">
<h2 class="content-title">聯(lián)系我們</h2>
<!-- 使用 router-link 的 to 對象寫法傳遞 query 參數(shù) -->
<div class="tab-buttons">
<router-link :to="{
path: '/contact',
query: { type: 'phone' }
}" class="tab-button">
電話聯(lián)系
</router-link>
<router-link :to="{
path: '/contact',
query: { type: 'email' }
}" class="tab-button">
郵箱聯(lián)系
</router-link>
<router-link :to="{
path: '/contact',
query: { type: 'address' }
}" class="tab-button">
地址信息
</router-link>
</div>
<!-- 子組件會通過路由自動接收 query 參數(shù) -->
<div class="content-area"><router-view></router-view></div>
</div>
</template>我們這里為每個路由鏈接添加一個query的type屬性,如果我們點擊這個按鈕的話,這個query就有type屬性的值了,這樣我們就可以被子組件收到了,然后就可以展示不同的內(nèi)容了,這里不要忘記在組件中使用router-view來渲染子路由的東西
- 父組件的也看下吧
<style scoped>
.contact-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.tab-buttons {
display: flex;
gap: 10px;
margin: 20px 0;
flex-wrap: wrap;
}
.tab-button {
padding: 10px 20px;
border: 1px solid #007bff;
background: white;
color: #007bff;
text-decoration: none;
border-radius: 5px;
cursor: pointer;
}
.tab-button:hover,
.tab-button.router-link-active {
background: #007bff;
color: white;
}
.content-area {
margin-top: 30px;
min-height: 200px;
}
</style>

到此這篇關(guān)于Vue3路由query參數(shù)的文章就介紹到這了,更多相關(guān)Vue路由query參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于vue項目中搜索節(jié)流的實現(xiàn)代碼
這篇文章主要介紹了關(guān)于vue項目中搜索節(jié)流的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
elementui源碼學(xué)習(xí)之仿寫一個el-divider組件
這篇文章主要為大家介紹了elementui源碼學(xué)習(xí)之仿寫一個el-divider組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
vue3不能使用history.pushState修改url參數(shù)踩坑
這篇文章主要為大家介紹了vue3不能使用history.pushState修改url參數(shù)踩坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
element tree懶加載:load="loadNode"只觸發(fā)一次的解決方案
本文主要介紹了element tree懶加載:load="loadNode"只觸發(fā)一次的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vue3使用TypeIt實現(xiàn)文字打字機效果的代碼示例
在現(xiàn)代網(wǎng)頁設(shè)計中,文字打字機效果是一種非常流行的動畫效果,能夠吸引用戶的注意力并提升用戶體驗,本文將介紹如何在 Vue 3 中使用 TypeIt 庫實現(xiàn)文字打字機效果,并分享一些實用的技巧和示例,需要的朋友可以參考下2025-01-01

