溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

VUE怎么實現(xiàn)路由傳遞參數(shù)

發(fā)布時間:2022-11-07 09:34:35 來源:億速云 閱讀:154 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“VUE怎么實現(xiàn)路由傳遞參數(shù)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

在路由時傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根據(jù)代碼看一下,VUE 和 Spring Boot 中各自是如何處理傳遞和接受參數(shù)的。

Spring Boot
package com.tang.demo1.controller; 
import org.springframework.web.bind.annotation.*; 
@RestController 
public class RouterController { 
 @RequestMapping(path = {"/router/{name}/{classid}"}, method = RequestMethod.GET) 
 public String router(@PathVariable("name") String name 
 ,@PathVariable("classid") int classid 
 ,@RequestParam(value = "type", defaultValue = "news") String type 
 ,@RequestParam(value = "num", required = falsef) int num){ 
 // 訪問 http://localhost:8080/router/tang/101?type=spor&num=12 
 return name + classid + type + num; 
 } 
} 

在url路徑中的,被稱為pathVariable,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

routes: [ 
 { 
 path: '/', 
 name: 'HomePage', 
 component: HomePage 
 }, 
 { 
 path: '/user/:id?/:type?', 
 name: 'User', 
 component: User 
 } 
 ]

首先在路由中配置url中需要傳遞的參數(shù),被稱為動態(tài)路徑參數(shù)。以“:”開始,末尾的“?”表示為可選的參數(shù)。

<template> 
<div> 
 <p>user</p> 
 <router-link :to="'/user/' + item.id + '/' + item.type +'?name=' + item.type" :key="index" v-for="(item, index) in userList">{{item.name}}</router-link> 
 <div v-if="childName"> 
 <p>-----</p> 
{{childName}} 
 </div> 
</div> 
</template> 
<script> 
var list = [ 
 {'name': 'xiaoming', 
 'id': 123, 
 'type': 'vip'}, 
 {'name': 'gangzi', 
 'id': 456, 
 'type': 'common'} 
] 
export default { 
 data(){ 
 return { 
 userList: list, 
 childName: null 
 } 
 }, 
 watch: { 
 $route(){ 
 if(this.$route.params.id){ 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 } 
 }, 
 methods: { 
 }, 
 created() { 
 // this.$route.params為動態(tài)路徑參數(shù) 
 if(this.$route.params.id){ 
// this.$route.params為查詢參數(shù) 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 }, 
 components: { 
 } 
}; 
</script>

vue中接受參數(shù)需要從routes實例中獲取,動態(tài)路徑參數(shù)在params里,查詢參數(shù)在query里。

當vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當其發(fā)生變化時,再獲取數(shù)據(jù)。

在路由時傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根據(jù)代碼看一下,VUE 和 Spring Boot 中各自是如何處理傳遞和接受參數(shù)的。

Spring Boot
package com.tang.demo1.controller; 
import org.springframework.web.bind.annotation.*; 
@RestController 
public class RouterController { 
 @RequestMapping(path = {"/router/{name}/{classid}"}, method = RequestMethod.GET) 
 public String router(@PathVariable("name") String name 
 ,@PathVariable("classid") int classid 
 ,@RequestParam(value = "type", defaultValue = "news") String type 
 ,@RequestParam(value = "num", required = falsef) int num){ 
 // 訪問 http://localhost:8080/router/tang/101?type=spor&num=12 
 return name + classid + type + num; 
 } 
}

在url路徑中的,被稱為pathVariable,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

routes: [ 
 { 
 path: '/', 
 name: 'HomePage', 
 component: HomePage 
 }, 
 { 
 path: '/user/:id?/:type?', 
 name: 'User', 
 component: User 
 } 
 ]

首先在路由中配置url中需要傳遞的參數(shù),被稱為動態(tài)路徑參數(shù)。以“:”開始,末尾的“?”表示為可選的參數(shù)。

<template> 
<div> 
 <p>user</p> 
 <router-link :to="'/user/' + item.id + '/' + item.type +'?name=' + item.type" :key="index" v-for="(item, index) in userList">{{item.name}}</router-link> 
 
 <div v-if="childName"> 
 <p>-----</p> 
{{childName}} 
 </div> 
</div> 
</template> 
<script> 
var list = [ 
 {'name': 'xiaoming', 
 'id': 123, 
 'type': 'vip'}, 
 {'name': 'gangzi', 
 'id': 456, 
 'type': 'common'} 
] 
export default { 
 data(){ 
 return { 
 userList: list, 
 childName: null 
 } 
 }, 
 watch: { 
 $route(){ 
 if(this.$route.params.id){ 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 } 
 }, 
 methods: { 
 
 }, 
 created() { 
 // this.$route.params為動態(tài)路徑參數(shù) 
 if(this.$route.params.id){ 
// this.$route.params為查詢參數(shù) 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 
 }, 
 components: { 
 } 
}; 
</script>

vue中接受參數(shù)需要從routes實例中獲取,動態(tài)路徑參數(shù)在params里,查詢參數(shù)在query里。

當vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當其發(fā)生變化時,再獲取數(shù)據(jù)。

在路由時傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根據(jù)代碼看一下,VUE 和 Spring Boot 中各自是如何處理傳遞和接受參數(shù)的。

Spring Boot
package com.tang.demo1.controller; 
import org.springframework.web.bind.annotation.*; 
@RestController 
public class RouterController { 
 @RequestMapping(path = {"/router/{name}/{classid}"}, method = RequestMethod.GET) 
 public String router(@PathVariable("name") String name 
 ,@PathVariable("classid") int classid 
 ,@RequestParam(value = "type", defaultValue = "news") String type 
 ,@RequestParam(value = "num", required = falsef) int num){ 
 // 訪問 http://localhost:8080/router/tang/101?type=spor&num=12 
 return name + classid + type + num; 
 } 
}

在url路徑中的,被稱為pathVariable,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

routes: [ 
 { 
 path: '/', 
 name: 'HomePage', 
 component: HomePage 
 }, 
 { 
 path: '/user/:id?/:type?', 
 name: 'User', 
 component: User 
 } 
 ]

首先在路由中配置url中需要傳遞的參數(shù),被稱為動態(tài)路徑參數(shù)。以“:”開始,末尾的“?”表示為可選的參數(shù)。

<template> 
<div> <p>user</p> 
 <router-link :to="'/user/' + item.id + '/' + item.type +'?name=' + item.type" :key="index" v-for="(item, index) in userList">{{item.name}}</router-link> 
 <div v-if="childName"> 
 <p>-----</p> 
{{childName}} 
 </div> 
</div> 
</template> 
<script> 
var list = [ 
 {'name': 'xiaoming', 
 'id': 123, 
 'type': 'vip'}, 
 {'name': 'gangzi', 
 'id': 456, 
 'type': 'common'} 
] 
export default { 
 data(){ 
 return { 
 userList: list, 
 childName: null 
 } 
 }, 
 watch: { 
 $route(){ 
 if(this.$route.params.id){ 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 } 
 }, 
 methods: { 
 }, 
 created() { 
 // this.$route.params為動態(tài)路徑參數(shù) 
 if(this.$route.params.id){ 
// this.$route.params為查詢參數(shù) 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 }, 
 components: { 
 } 
}; 
</script>

vue中接受參數(shù)需要從routes實例中獲取,動態(tài)路徑參數(shù)在params里,查詢參數(shù)在query里。

當vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當其發(fā)生變化時,再獲取數(shù)據(jù)。

Vue的優(yōu)點

Vue具體輕量級框架、簡單易學、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。

“VUE怎么實現(xiàn)路由傳遞參數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI