vue跳轉(zhuǎn)頁(yè)面打開(kāi)新窗口,并攜帶與接收參數(shù)方式
1、攜帶普通類型參數(shù)
字符串、數(shù)字等。
path:要跳轉(zhuǎn)新頁(yè)面的路由鏈接
query:要攜帶的參數(shù)
let pathInfo = this.$router.resolve({
? path:'/product_detail',
? ? ?query:{
? ? ? ? ?productId:'11'
? ? ?}
?})
?window.open(pathInfo.href, '_blank');新頁(yè)面的參數(shù)接收:
this.productId = this.$route.query.productId
2、攜帶復(fù)雜類型參數(shù)
對(duì)象、數(shù)組等,通過(guò)JSON轉(zhuǎn)換進(jìn)行傳遞。
let pathInfo = this.$router.resolve({
? ?path:'/product_detail',
? ? ?query:{
? ? ? ? ?data:{name:'張三'}
? ? ?}
?})
?window.open(pathInfo.href, '_blank');新頁(yè)面的參數(shù)接收:
console.log(this.$route.query.data)
vue頁(yè)面跳轉(zhuǎn)并傳參的八種方式
我們知道,在vue中每個(gè)頁(yè)面都需要在路由中聲明,就是在router/index.js中寫下面代碼:
import Vue from 'vue'
import Router from 'vue-router'
import Test from "../components/Test";
Vue.use(Router)
export default new Router({
? mode: 'history',
? routes: [
?? ??? ? ?{
?? ??? ? ? ? ?path: '/t',
?? ??? ? ? ? ?name: 'Test',
?? ??? ? ? ? ?component: Test,
?? ??? ? ? ? ?hidden:true
?? ??? ? ? ?},
? ? ?? ?]
? ? })實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)并傳參有多種方式:
方法一
在template中可以使用<router-link>標(biāo)簽實(shí)現(xiàn)跳轉(zhuǎn),跳轉(zhuǎn)的路徑是http://localhost:8080/t?index=id,如下:
<router-link to="/t?index=1"> ? ? ?<button class="btn btn-default">點(diǎn)擊跳轉(zhuǎn)</button> </router-link>
只需要點(diǎn)擊按鈕就可以實(shí)現(xiàn)跳轉(zhuǎn),不需要寫js代碼,需要傳遞參數(shù)的話只需要/t?index=1即可,這樣的話跳轉(zhuǎn)的頁(yè)面獲取參數(shù)通過(guò)window.location.href能夠獲取到完整的url,然后截取參數(shù)。也可以通過(guò)下面代碼獲取參數(shù)
this.$route.query.index
方法二
跳轉(zhuǎn)的路徑是http://localhost:8080/t?index=id
<router-link :to="{path:'/t',query: {index: 1}}">
? ? ?<button class="btn btn-default">點(diǎn)擊跳轉(zhuǎn)</button>
</router-link>其中需要注意,這里的to前面一定要加冒號(hào),path的值要和上面路由定義的值一致,傳參用query,里面是參數(shù)字典。
接收參數(shù):
this.$route.query.index
方法三
命名路由的方式:
跳轉(zhuǎn)的路徑是http://localhost:8080/t?index=id
<router-link :to="{name:'Test',params: {index: 1}}">
? ? ?<button class="btn btn-default">點(diǎn)擊跳轉(zhuǎn)</button>
</router-link>注意這里的name也要和router/index.js中聲明的name值一致,并且傳參使用params,和name配對(duì)的是params,和path配對(duì)的是query。
接收參數(shù):
this.$route.params.index
方法四
跳轉(zhuǎn)的路徑是http://localhost:8080/t/id
<router-link:to="'/test/'+1"> ? ? ?<button class="btn btn-default">點(diǎn)擊跳轉(zhuǎn)</button> </router-link>
這時(shí)的路由也需要更為為下面的形式:
routes: [
?? ??? ? ?{
?? ??? ? ? ? ?path: '/t/:index',
?? ??? ? ? ? ?name: 'Test',
?? ??? ? ? ? ?component: Test,
?? ??? ? ? ? ?hidden:true
?? ??? ? ? ?},
? ? ?? ?]接收參數(shù):
this.$route.params.index
方法五
上面四種方法都是在html中實(shí)現(xiàn)的跳轉(zhuǎn),還有另外對(duì)應(yīng)的在js中實(shí)現(xiàn)的跳轉(zhuǎn)并傳參的方法,代碼如下:
<template>
<button @click = "func()">跳轉(zhuǎn)</button>
</template>
<script>
? ? export default{
? ? ? ? methods:{
? ? ? ? ? ? func (){
? ? ? ? ? ? ? ? this.$router.push({path: '/t?index=1'});
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>接收參數(shù)依然使用
this.$route.query.index
方法六
<template>
<button @click = "func()">跳轉(zhuǎn)</button>
</template>
<script>
? ? export default{
? ? ? ? methods:{
? ? ? ? ? ? func (){
? ? ? ? ? ? ? ? this.$router.push({path: '/t',query:{ index:'1'}});
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
接收參數(shù)依然使用
this.$route.query.index
方法七
<template>
<button @click = "func()">跳轉(zhuǎn)</button>
</template>
<script>
? ? export default{
? ? ? ? methods:{
? ? ? ? ? ? func (){
? ? ? ? ? ? ? ? this.$router.push({path: '/t/index'});
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
接收參數(shù)依然使用
this.$route.query.index
方法八
<template>
<button @click = "func()">跳轉(zhuǎn)</button>
</template>
<script>
? ? export default{
? ? ? ? methods:{
? ? ? ? ? ? func (){
? ? ? ? ? ? ? ? this.$router.push({name: 'Test',params:{ index:'1'}});
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>接收參數(shù)依然使用
this.$route.params.index
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue用addRoutes實(shí)現(xiàn)動(dòng)態(tài)路由的示例
本篇文章主要介紹了vue用addRoutes實(shí)現(xiàn)動(dòng)態(tài)路由的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Vue實(shí)現(xiàn)pdf在線預(yù)覽功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)pdf在線預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
nuxt中刷新頁(yè)面后防止store值丟失問(wèn)題
這篇文章主要介紹了nuxt中刷新頁(yè)面后防止store值丟失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
el-dialog關(guān)閉再打開(kāi)后窗口內(nèi)容不刷新問(wèn)題及解決
這篇文章主要介紹了el-dialog關(guān)閉再打開(kāi)后窗口內(nèi)容不刷新問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式
如今的Vue3已經(jīng)勢(shì)不可擋,當(dāng)然搭建一個(gè)全新的Vue3項(xiàng)目也有了全新的方式,下面這篇文章主要給大家介紹了關(guān)于如何手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換
這篇文章主要為大家詳細(xì)介紹了vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Vue+WebSocket頁(yè)面實(shí)時(shí)刷新長(zhǎng)連接的實(shí)現(xiàn)
最近vue項(xiàng)目要做數(shù)據(jù)實(shí)時(shí)刷新,數(shù)據(jù)較大,會(huì)出現(xiàn)卡死情況,所以本文主要介紹了頁(yè)面實(shí)時(shí)刷新長(zhǎng)連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06

