Vue3之路由跳轉(zhuǎn)與參數(shù)獲取方式
Vue3路由跳轉(zhuǎn)與參數(shù)獲取
路由跳轉(zhuǎn)
import { useRouter } from "vue-router"
export default {
? setup () {
? ? const router = useRouter(); // 第一步
? ? const state = reactive({
? ? ? list: [],
? ? ? toDeatil(id) {
? ? ? ? router.push('/product?id=' + id); // 第二步
? ? ? },
? ? })
? ? return {
? ? ? ...toRefs(state)
? ? }
? }
}參數(shù)獲取
import { useRoute } from "vue-router"
export default {
? setup () {
? ? const route = useRoute(); // 第一步
? ? console.log(route.query.type); // 第二步
? ? const state = reactive({
? ? ? list: [],
? ? })
? ? return {
? ? ? ...toRefs(state)
? ? }
? }
}Vue2和Vue3的路由跳轉(zhuǎn)及路由傳參
首先,路由跳轉(zhuǎn)的方法有兩種:
聲明式導(dǎo)航: router-link (務(wù)必要有to屬性),可以實現(xiàn)路由跳轉(zhuǎn)
編程式導(dǎo)航: 利用的是組件實例的$router.push|replace方法,可以實現(xiàn)路由的跳轉(zhuǎn) (可以書寫一些自己的業(yè)務(wù))
示例:
點擊logo按鈕,跳轉(zhuǎn)到home頁 (聲明式)
<router-link class="logo" to="/home">
<img src="./images/logo.png" alt="">
</router-link>點擊搜索按鈕,執(zhí)行跳轉(zhuǎn)( goSearch ) (編程式)
<button class="btn" type="button" @click="goSearch">搜索</button>
這里的話,Vue2和Vue3寫法有點不一樣
Vue2中就是methods里面定義goSearch()
methods: {
goSearch(){
this.$router.push('/search');
}
}Vue3可以使用setup語法糖
<script>
import { useRouter } from 'vue-router'
export default {
name: 'HeaderIndex',
setup(){
const $router = useRouter()
// method
function goSearch(){
$router.push('/search');
}
return {
goSearch
}
}
}
</script>接著再寫一下路由傳參,那參數(shù)一共有2種:
- params參數(shù): 屬于路徑當中的一部分,需要注意,在配置路由的時候,需要占位
- query參數(shù): 不屬于路徑當中的一部分,類似于ajax中的queryString /home?k=v$kv=,不需要占位
比如,在搜索框中輸入一些內(nèi)容,點擊搜索按鈕,完成路由傳參跳轉(zhuǎn)

接下來都是以Vue3中為例,最后再寫上vue2中的相應(yīng)代碼
第一種:字符串
給搜索框進行雙向綁定keyword
<form action="###" class="searchForm">
<input type="text" v-model="keyword"/>
<button class="btn" type="button" @click="goSearch">搜索</button>
</form>首先,如果要使用params參數(shù)的話,需要配置路由時進行占位
const routes = [
{
path:'/search/:keyword',
component: SearchIndex,
}然后使用字符串形式傳遞參數(shù)
<script>
import { useRouter } from 'vue-router'
import {ref} from 'vue'
export default {
name: 'HeaderIndex',
setup(){
const $router = useRouter()
let keyword = ref('')
// method
function goSearch(){
// 路由傳參
// 第一種:字符串形式
$router.push('/search/' + keyword.value + "?k=" + keyword.value.toUpperCase());
}
return {
goSearch,
keyword
}
}
}
</script>

因為我們使用了ref,所以接收參數(shù)的時候要 .value來獲取,不然是讀不到的。
- /asd 就是 params參數(shù)
- ASD 就是 query參數(shù)
- 我們可以通過輸出來驗證
我在SearchIndex.vue中通過模版字符串將它顯示
<template>
<div>
我是搜索
<h1>params參數(shù)---{{$route.params.keyword}}</h1>
<h1>query參數(shù)---{{$route.query.k}}</h1>
</div>
</template> 
第二種:模版字符串
這種寫法比第一種字符串形式會簡單一些,要替換的就是下面這串,其余不變。就是用模版字符串來代替字符串拼接而已
// 第二種:模版字符串
$router.push(`/search/${keyword.value}?k=${keyword.value.toUpperCase()}`)第三種:對象寫法
這種寫法是最常用的,也是最直觀的
但如果使用對象寫法,并且還是用的params參數(shù),就不能用path形式了,而是要用name形式
router/index.js中對search的路由添加name。不再使用path
{
path:'/search/:keyword',
component: SearchIndex,
meta:{show:true},
name:"search"
}function goSearch(){
console.log(keyword.value);
// 路由傳參
// 第一種:字符串形式
// $router.push('/search/' + keyword.value + "?k=" + keyword.value.toUpperCase());
// 第二種:模版字符串
// $router.push(`/search/${keyword.value}?k=${keyword.value.toUpperCase()}`)
// 第三種:對象寫法
$router.push({
name:"search",
params:{keyword:keyword.value},
query:{k:keyword.value.toUpperCase()}
})
}而Vue2中的寫法其實大同小異,就是沒有setup和ref。
function goSearch(){
// 路由傳參
// 第一種:字符串形式
// this.$router.push('/search/' + this.keyword + "?k=" + this.keyword.toUpperCase());
// 第二種:模版字符串
// this.$router.push(`/search/${this.keyword}?k=${this.keyword.toUpperCase()}`)
// 第三種:對象寫法
this.$router.push({
name:"search",
params:{keyword:this.keyword},
query:{k:this.keyword.toUpperCase()}
})
}就是訪問keyword時需要this訪問,并且由于沒有ref以后也不需要加上value了
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Pinia Persistedstate插件實現(xiàn)狀態(tài)持久化的操作方法
Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開發(fā)者的喜愛,然而,Pinia 默認使用內(nèi)存存儲狀態(tài),為了解決這個問題,我們可以借助 Pinia Persistedstate 插件來實現(xiàn)狀態(tài)的持久化存儲,本文將詳細介紹該插件的使用方法,需要的朋友可以參考下2024-11-11
vue3使用defineModel實現(xiàn)父子組件雙向綁定
這篇文章主要個給大家介紹了在vue3中使用defineModel進行父子組件中的雙向綁定,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
vue如何實現(xiàn)路由跳轉(zhuǎn)到外部鏈接界面
這篇文章主要介紹了vue如何實現(xiàn)路由跳轉(zhuǎn)到外部鏈接界面,具有很好的參考價值,希望對大家有所幫助。2022-10-10
vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面
本文給大家分享了vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面的一個功能,有這方便需要的朋友學(xué)習(xí)參考下吧。2018-01-01
vue使用vuex實現(xiàn)首頁導(dǎo)航切換不同路由的方法
這篇文章主要介紹了vue使用vuex實現(xiàn)首頁導(dǎo)航切換不同路由的方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

