this.$router.push攜帶參數(shù)跳轉(zhuǎn)頁面的實(shí)現(xiàn)代碼
前言
this.$router.push進(jìn)行頁面跳轉(zhuǎn)時(shí)。攜帶參數(shù)有params和query兩種方式。
一、params和query使用方式
query方式:
this. r o u t e r . p u s h ( p a t h : ′ t e s t Q u e r y ′ , q u e r y : t e s t Q u e r y : ′ t e s t Q u e r y ′ ) , 傳 遞 的 參 數(shù) 會(huì) 拼 接 在 跳 轉(zhuǎn) 地 址 的 后 面 。 使 用 t h i s . router.push({path: 'testQuery',query: {testQuery:'testQuery'}}),傳遞的參數(shù)會(huì)拼接在跳轉(zhuǎn)地址的后面。使用this. router.push(path:′testQuery′,query:testQuery:′testQuery′),傳遞的參數(shù)會(huì)拼接在跳轉(zhuǎn)地址的后面。使用this.route.params.key取值
params方式:
this. r o u t e r . p u s h ( n a m e : ′ t e s t P a r a m s ′ , p a r a m s : t e s t P a r a m s : ′ t e s t P a r a m s ′ ) , 傳 遞 的 參 數(shù) 不 會(huì) 拼 接 在 跳 轉(zhuǎn) 的 后 面 。 使 用 t h i s . router.push({name: 'testParams',params: {testParams:'testParams'}}),傳遞的參數(shù)不會(huì)拼接在跳轉(zhuǎn)的后面。使用this. router.push(name:′testParams′,params:testParams:′testParams′),傳遞的參數(shù)不會(huì)拼接在跳轉(zhuǎn)的后面。使用this.route.query.key取值
二、實(shí)現(xiàn)代碼
1.index.js代碼
const router = new Router({
routes: [
{
path:'/test',
component: test,
},
{
name: 'testParams',
path:'/testParams',
component: TestParams,
},
{
path:'/testQuery',
component: TestQuery,
}
]
})2.test.vue代碼
<template>
<div class="test">
<button v-on:click="testParams">params</button>
<button v-on:click="testQuery">query</button>
</div>
</template>
<script>
export default {
name: "test",
data(){
return {
}
},
methods:{
testParams(){
this.$router.push({name: 'testParams',params: {testParams:'testParams'}});
},
testQuery(){
this.$router.push({path: 'testQuery',query: {testQuery:'testQuery'}});
}
}
}
</script>3.testParams代碼
<template>
<div id="testParams">
{{testParams}}
</div>
</template>
<script>
export default {
name: "TestParams",
data() {
return{
testParams: ''
}
},mounted() {
this.testParams = this.$route.params.testParams;
}
}
</script>4.testParams代碼
<template>
<div id="testQuery">
{{testQuery}}
</div>
</template>
<script>
export default {
name: "TestQuery",
data(){
return{
testQuery: ''
}
},mounted() {
this.testQuery = this.$route.query.testQuery;
}
}
</script>5.效果



總結(jié)
兩種方式非常相似,區(qū)別在于兩點(diǎn):
1、是否在地址后面拼接參數(shù)。
2、因?yàn)閯?dòng)態(tài)路由也是傳遞params的,所以在 this.$router.push() 方法中 path是不能和params一起使用的,否則params將無效。需要用name來指定頁面,一定要記得,在index.js中設(shè)置好那么屬性。
到此這篇關(guān)于this.$router.push攜帶參數(shù)跳轉(zhuǎn)頁面的文章就介紹到這了,更多相關(guān)this.$router.push跳轉(zhuǎn)頁面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE?axios每次請(qǐng)求添加時(shí)間戳問題
這篇文章主要介紹了VUE?axios每次請(qǐng)求添加時(shí)間戳問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue3中setup語法糖下通用的分頁插件實(shí)例詳解
這篇文章主要介紹了vue3中setup語法糖下通用的分頁插件,實(shí)例代碼介紹了自定義分頁插件:PagePlugin.vue,文中提到了vue3中setup語法糖下父子組件之間的通信,需要的朋友可以參考下2022-10-10
vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
ant-design-vue中設(shè)置Table每頁顯示的條目數(shù)量方式
這篇文章主要介紹了ant-design-vue中設(shè)置Table每頁顯示的條目數(shù)量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

