Spring Boot/VUE中路由傳遞參數(shù)的實(shí)現(xiàn)代碼
在路由時(shí)傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢(xún)參數(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){ // 訪問(wèn) http://localhost:8080/router/tang/101?type=spor&num=12 return name + classid + type + num; } } 在url路徑中的,被稱(chēng)為pathVariable,查詢(xún)參數(shù)被稱(chēng)為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。 VUE routes: [ { path: '/', name: 'HomePage', component: HomePage }, { path: '/user/:id?/:type?', name: 'User', component: User } ]
首先在路由中配置url中需要傳遞的參數(shù),被稱(chēng)為動(dòng)態(tài)路徑參數(shù)。以“:”開(kāi)始,末尾的“?”表示為可選的參數(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為動(dòng)態(tài)路徑參數(shù) if(this.$route.params.id){ // this.$route.params為查詢(xún)參數(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實(shí)例中獲取,動(dòng)態(tài)路徑參數(shù)在params里,查詢(xún)參數(shù)在query里。
當(dāng)vue的動(dòng)態(tài)路徑組件處在激活狀態(tài)時(shí),如果改變動(dòng)態(tài)路徑參數(shù),那么寫(xiě)在created()的方法將不會(huì)再次被調(diào)用,因?yàn)樵摻M件已經(jīng)創(chuàng)建好了。此時(shí),可以為$route添加一個(gè)watch,當(dāng)其發(fā)生變化時(shí),再獲取數(shù)據(jù)。
在路由時(shí)傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢(xún)參數(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){ // 訪問(wèn) http://localhost:8080/router/tang/101?type=spor&num=12 return name + classid + type + num; } }
在url路徑中的,被稱(chēng)為pathVariable,查詢(xún)參數(shù)被稱(chēng)為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。
VUE
routes: [ { path: '/', name: 'HomePage', component: HomePage }, { path: '/user/:id?/:type?', name: 'User', component: User } ]
首先在路由中配置url中需要傳遞的參數(shù),被稱(chēng)為動(dòng)態(tài)路徑參數(shù)。以“:”開(kāi)始,末尾的“?”表示為可選的參數(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為動(dòng)態(tài)路徑參數(shù) if(this.$route.params.id){ // this.$route.params為查詢(xún)參數(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實(shí)例中獲取,動(dòng)態(tài)路徑參數(shù)在params里,查詢(xún)參數(shù)在query里。
當(dāng)vue的動(dòng)態(tài)路徑組件處在激活狀態(tài)時(shí),如果改變動(dòng)態(tài)路徑參數(shù),那么寫(xiě)在created()的方法將不會(huì)再次被調(diào)用,因?yàn)樵摻M件已經(jīng)創(chuàng)建好了。此時(shí),可以為$route添加一個(gè)watch,當(dāng)其發(fā)生變化時(shí),再獲取數(shù)據(jù)。
在路由時(shí)傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢(xún)參數(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){ // 訪問(wèn) http://localhost:8080/router/tang/101?type=spor&num=12 return name + classid + type + num; } }
在url路徑中的,被稱(chēng)為pathVariable,查詢(xún)參數(shù)被稱(chēng)為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。
VUE
routes: [ { path: '/', name: 'HomePage', component: HomePage }, { path: '/user/:id?/:type?', name: 'User', component: User } ]
首先在路由中配置url中需要傳遞的參數(shù),被稱(chēng)為動(dòng)態(tài)路徑參數(shù)。以“:”開(kāi)始,末尾的“?”表示為可選的參數(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為動(dòng)態(tài)路徑參數(shù) if(this.$route.params.id){ // this.$route.params為查詢(xún)參數(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實(shí)例中獲取,動(dòng)態(tài)路徑參數(shù)在params里,查詢(xún)參數(shù)在query里。
當(dāng)vue的動(dòng)態(tài)路徑組件處在激活狀態(tài)時(shí),如果改變動(dòng)態(tài)路徑參數(shù),那么寫(xiě)在created()的方法將不會(huì)再次被調(diào)用,因?yàn)樵摻M件已經(jīng)創(chuàng)建好了。此時(shí),可以為$route添加一個(gè)watch,當(dāng)其發(fā)生變化時(shí),再獲取數(shù)據(jù)。
總結(jié)
以上所述是小編給大家介紹的Spring Boot/VUE中路由傳遞參數(shù)的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- springboot控制層傳遞參數(shù)為非必填值的操作
- springboot websocket集群(stomp協(xié)議)連接時(shí)候傳遞參數(shù)
- Spring?Boot獲取resources目錄下的文件三種方式詳解
- Springboot中com.mysql.cj.jdbc.Driver在yml文件中爆紅的原因解讀
- SpringBoot之如何指定配置文件啟動(dòng)
- springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄(思路詳解)
- springboot項(xiàng)目中引入本地依賴(lài)jar包并打包到lib文件夾中
- spring boot項(xiàng)目同時(shí)傳遞參數(shù)和文件的多種方式代碼演示
相關(guān)文章
Vuex中Store的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Vuex中Store的簡(jiǎn)單實(shí)現(xiàn),為了在 Vue 組件中訪問(wèn) this.$store property,你需要為 Vue 實(shí)例提供創(chuàng)建好的 store,Vuex 提供了一個(gè)從根組件向所有子組件,以 store 選項(xiàng)的方式 注入 該 store 的機(jī)制,需要的朋友可以參考下2023-11-11Vue?UI創(chuàng)建項(xiàng)目詳細(xì)步驟
本文主要介紹了Vue?UI創(chuàng)建項(xiàng)目詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01element-plus結(jié)合sortablejs實(shí)現(xiàn)table行拖拽效果
使用element-plus的el-table組件創(chuàng)建出來(lái)的table,結(jié)合sortable.js實(shí)現(xiàn)table行拖動(dòng)排序,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的同學(xué)可以自己動(dòng)手試一試2023-10-10Vue+Echarts實(shí)現(xiàn)簡(jiǎn)單折線圖
這篇文章主要為大家詳細(xì)介紹了Vue+Echarts實(shí)現(xiàn)簡(jiǎn)單折線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03頁(yè)面內(nèi)錨點(diǎn)定位及跳轉(zhuǎn)方法總結(jié)(推薦)
這篇文章主要介紹了頁(yè)面內(nèi)錨點(diǎn)定位及跳轉(zhuǎn)方法總結(jié),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04vue-cli3 打包優(yōu)化之 splitchunks詳解
這篇文章主要介紹了vue-cli3 打包優(yōu)化之 splitchunks的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07拿來(lái)就用vue-gird-layout組件封裝示例
這篇文章主要介紹了vue-gird-layout組件封裝示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08vue點(diǎn)擊按鈕跳轉(zhuǎn)到另一個(gè)vue頁(yè)面實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue點(diǎn)擊按鈕跳轉(zhuǎn)到另一個(gè)vue頁(yè)面的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Vue2利用Axios發(fā)起請(qǐng)求的詳細(xì)過(guò)程記錄
有很多時(shí)候你在構(gòu)建應(yīng)用時(shí)需要訪問(wèn)一個(gè)API并展示其數(shù)據(jù),做這件事的方法有好幾種,而使用基于promise的HTTP客戶端axios則是其中非常流行的一種,這篇文章主要給大家介紹了關(guān)于Vue2利用Axios發(fā)起請(qǐng)求的詳細(xì)過(guò)程,需要的朋友可以參考下2021-12-12