当前位置: 首页 > 编程笔记 >

Spring Boot/VUE中路由传递参数的实现代码

葛宏爽
2023-03-14
本文向大家介绍Spring Boot/VUE中路由传递参数的实现代码,包括了Spring Boot/VUE中路由传递参数的实现代码的使用技巧和注意事项,需要的朋友参考一下

在路由时传递参数,一般有两种形式,一种是拼接在url地址中,另一种是查询参数。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根据代码看一下,VUE 和 Spring Boot 中各自是如何处理传递和接受参数的。

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,查询参数被称为pequestParm。在controller中接受参数,可以直接在方法里用了。

VUE

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

首先在路由中配置url中需要传递的参数,被称为动态路径参数。以“:”开始,末尾的“?”表示为可选的参数。

<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为动态路径参数 
 if(this.$route.params.id){ 
// this.$route.params为查询参数 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 }, 
 components: { 
 } 
}; 
</script> 

vue中接受参数需要从routes实例中获取,动态路径参数在params里,查询参数在query里。

当vue的动态路径组件处在激活状态时,如果改变动态路径参数,那么写在created()的方法将不会再次被调用,因为该组件已经创建好了。此时,可以为$route添加一个watch,当其发生变化时,再获取数据。

在路由时传递参数,一般有两种形式,一种是拼接在url地址中,另一种是查询参数。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根据代码看一下,VUE 和 Spring Boot 中各自是如何处理传递和接受参数的。

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,查询参数被称为pequestParm。在controller中接受参数,可以直接在方法里用了。

VUE

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

首先在路由中配置url中需要传递的参数,被称为动态路径参数。以“:”开始,末尾的“?”表示为可选的参数。

<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为动态路径参数 
 if(this.$route.params.id){ 
// this.$route.params为查询参数 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 
 }, 
 components: { 
 } 
}; 
</script> 

vue中接受参数需要从routes实例中获取,动态路径参数在params里,查询参数在query里。

当vue的动态路径组件处在激活状态时,如果改变动态路径参数,那么写在created()的方法将不会再次被调用,因为该组件已经创建好了。此时,可以为$route添加一个watch,当其发生变化时,再获取数据。

在路由时传递参数,一般有两种形式,一种是拼接在url地址中,另一种是查询参数。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根据代码看一下,VUE 和 Spring Boot 中各自是如何处理传递和接受参数的。

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,查询参数被称为pequestParm。在controller中接受参数,可以直接在方法里用了。

VUE

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

首先在路由中配置url中需要传递的参数,被称为动态路径参数。以“:”开始,末尾的“?”表示为可选的参数。

<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为动态路径参数 
 if(this.$route.params.id){ 
// this.$route.params为查询参数 
this.childName = this.$route.params.id +'//////' + this.$route.query.name; 
 }else{ 
 this.childName = null 
 } 
 }, 
 deactivated() { 
 console.log('deact') 
 }, 
 computed: { 
 }, 
 components: { 
 } 
}; 
</script> 

vue中接受参数需要从routes实例中获取,动态路径参数在params里,查询参数在query里。

当vue的动态路径组件处在激活状态时,如果改变动态路径参数,那么写在created()的方法将不会再次被调用,因为该组件已经创建好了。此时,可以为$route添加一个watch,当其发生变化时,再获取数据。

总结

以上所述是小编给大家介绍的Spring Boot/VUE中路由传递参数的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对小牛知识库网站的支持!

 类似资料:
  • 例如,在具有分页列表的路由上,网址可能如下所示,表示我们已加载第二个网页: 使用指令和来传递查询参数。例如: 或者,我们可以使用服务通过JS跳转: 读取查询参数 See Official Documentation on Query Parameters

  • 本文向大家介绍ajax传递多个参数的实现代码,包括了ajax传递多个参数的实现代码的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了ajax传递多个参数的具体代码,供大家参考,具体内容如下 WebService1.asmx 以上就是本文的全部内容,希望对大家的学习有所帮助。

  • 本文向大家介绍vue-router 路由传参用法实例分析,包括了vue-router 路由传参用法实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了vue-router 路由传参用法。分享给大家供大家参考,具体如下: 在设置路由规则时,我们可以给路径名设置一个别名,方便进行路由跳转,而不需要去记住过长的全路径。 例如: 上文中的 importFile,jsp 在上一篇路由基本用法中介

  • 本文向大家介绍vue路由权限校验功能的实现代码,包括了vue路由权限校验功能的实现代码的使用技巧和注意事项,需要的朋友参考一下 引言 做后台系统的时候,难免会有用户权限的判断。admin可以查看全部菜单,user只能查看部分菜单。 一开始接触这个需求的时候,完全是纯前端做的。在配置路由的时候,加一个roles的属性,通过判断用户的roles是否与路由的roles属性相匹配来作为显示隐藏的依据 这样

  • 本文向大家介绍浅谈angular4.0中路由传递参数、获取参数最nice的写法,包括了浅谈angular4.0中路由传递参数、获取参数最nice的写法的使用技巧和注意事项,需要的朋友参考一下 研究ng4的官网,终于找到了我想要的方法。我想要的结果是用‘&'拼接参数传送,这样阅读上是最好的。 否则很多‘/'的拼接,容易混淆参数和组件名称。 一般我们页面跳转传递参数都是这样的格式: http://an

  • 我正在尝试使用http://localhost:3000/login?Id=1这样的查询参数来实现React路由器,我只能在我的登录路由中实现它,如果我将路径作为http://localhost:3000/然后重定向,但是,我想在整个应用程序中实现。如果我在其他路由上实现,它会匹配路由。这就是我的的样子,有人能指导我如何实现包括查询参数在内的所有路由路径吗?