Spring Boot/VUE中路由傳遞參數(shù)的實現(xiàn)代碼
在路由時傳遞參數(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里。
當(dāng)vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當(dāng)其發(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里。
當(dāng)vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當(dāng)其發(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里。
當(dāng)vue的動態(tài)路徑組件處在激活狀態(tài)時,如果改變動態(tài)路徑參數(shù),那么寫在created()的方法將不會再次被調(diào)用,因為該組件已經(jīng)創(chuàng)建好了。此時,可以為$route添加一個watch,當(dāng)其發(fā)生變化時,再獲取數(shù)據(jù)。
總結(jié)
以上所述是小編給大家介紹的Spring Boot/VUE中路由傳遞參數(shù)的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- springboot控制層傳遞參數(shù)為非必填值的操作
- springboot websocket集群(stomp協(xié)議)連接時候傳遞參數(shù)
- Spring?Boot獲取resources目錄下的文件三種方式詳解
- Springboot中com.mysql.cj.jdbc.Driver在yml文件中爆紅的原因解讀
- SpringBoot之如何指定配置文件啟動
- springboot實現(xiàn)jar運行復(fù)制resources文件到指定的目錄(思路詳解)
- springboot項目中引入本地依賴jar包并打包到lib文件夾中
- spring boot項目同時傳遞參數(shù)和文件的多種方式代碼演示
相關(guān)文章
element-plus結(jié)合sortablejs實現(xiàn)table行拖拽效果
使用element-plus的el-table組件創(chuàng)建出來的table,結(jié)合sortable.js實現(xiàn)table行拖動排序,文中有詳細的代碼示例供大家參考,具有一定的參考價值,感興趣的同學(xué)可以自己動手試一試2023-10-10頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié)(推薦)
這篇文章主要介紹了頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04vue-cli3 打包優(yōu)化之 splitchunks詳解
這篇文章主要介紹了vue-cli3 打包優(yōu)化之 splitchunks的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07vue點擊按鈕跳轉(zhuǎn)到另一個vue頁面實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue點擊按鈕跳轉(zhuǎn)到另一個vue頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08