本篇內(nèi)容主要講解“Spring Boot/VUE中怎么實現(xiàn)路由傳遞參數(shù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Spring Boot/VUE中怎么實現(xiàn)路由傳遞參數(shù)”吧!
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設服務,提供網(wǎng)站制作、網(wǎng)站建設網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設計,自適應網(wǎng)站建設,網(wǎng)頁設計師打造企業(yè)風格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務。歡迎咨詢做網(wǎng)站需要多少錢:18980820575
在路由時傳遞參數(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ù)。
到此,相信大家對“Spring Boot/VUE中怎么實現(xiàn)路由傳遞參數(shù)”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
當前標題:SpringBoot/VUE中怎么實現(xiàn)路由傳遞參數(shù)
瀏覽路徑:http://jinyejixie.com/article24/jjehje.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、品牌網(wǎng)站設計、品牌網(wǎng)站制作、移動網(wǎng)站建設、服務器托管、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)