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ù): 屬于路徑當(dāng)中的一部分,需要注意,在配置路由的時候,需要占位
- query參數(shù): 不屬于路徑當(dāng)中的一部分,類似于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)文章
使用VUE實現(xiàn)在table中文字信息超過5個隱藏鼠標(biāo)移到時彈窗顯示全部
這篇文章主要介紹了使用VUE實現(xiàn)在table中文字信息超過5個隱藏,鼠標(biāo)移到時彈窗顯示全部,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Vue.js+Layer表格數(shù)據(jù)綁定與實現(xiàn)更新的實例
下面小編就為大家分享一篇Vue.js+Layer表格數(shù)據(jù)綁定與實現(xiàn)更新的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue包大小優(yōu)化的實現(xiàn)(從1.72M到94K)
這篇文章主要介紹了Vue包大小優(yōu)化的實現(xiàn)(從1.72M到94K),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
基于Vue3.0開發(fā)輕量級手機端彈框組件V3Popup的場景分析
這篇文章主要介紹了基于Vue3.0開發(fā)輕量級手機端彈框組件V3Popup,本文通過場景分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

