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


Posted in Javascript onMarch 02, 2018

在路由时传递参数,一般有两种形式,一种是拼接在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中路由传递参数的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
JavaScript高级程序设计(第3版)学习笔记9 js函数(下)
Oct 11 Javascript
自制的文件上传JS控件可支持IE、chrome、firefox etc
Apr 18 Javascript
jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
Mar 24 Javascript
js实现每日自动换一张图片的方法
May 04 Javascript
Highcharts 多个Y轴动态刷新数据的实现代码
May 28 Javascript
js自定义瀑布流布局插件
May 16 Javascript
基于Vue过渡状态实例讲解
Sep 14 Javascript
input type=file 选择图片并且实现预览效果的实例
Oct 26 Javascript
十分钟带你快速了解React16新特性
Nov 10 Javascript
vue移动端实现手机左右滑动入场动画
Jun 17 Javascript
VUE+elementui组件在table-cell单元格中绘制微型echarts图
Apr 20 Javascript
Ajax是什么?Ajax高级用法之Axios技术
Apr 21 Javascript
vue项目tween方法实现返回顶部的示例代码
Mar 02 #Javascript
Vue2.0系列之过滤器的使用
Mar 01 #Javascript
浅谈Vue Element中Select下拉框选取值的问题
Mar 01 #Javascript
vue.js 获取select中的value实例
Mar 01 #Javascript
Vue中的slot使用插槽分发内容的方法
Mar 01 #Javascript
在vue中封装可复用的组件方法
Mar 01 #Javascript
JavaScript判断日期时间差的实例代码
Mar 01 #Javascript
You might like
php基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
谈PHP生成静态页面分析 模板+缓存+写文件
2009/08/17 PHP
PHP APC配置文件2套和参数详解
2014/06/11 PHP
PHP中cookie和session的区别实例分析
2014/08/28 PHP
php显示时间常用方法小结
2015/06/05 PHP
PHP实现的mysql操作类【MySQL与MySQLi方式】
2017/10/07 PHP
JS/jQuery实现默认显示部分文字点击按钮显示全部内容
2013/05/13 Javascript
JavaScript-RegExp对象只能使用一次问题解决方法
2014/06/23 Javascript
js从Cookies里面取值的简单实现
2014/06/30 Javascript
Backbone.js中的集合详解
2015/01/14 Javascript
javascript日期比较方法实例分析
2016/06/17 Javascript
jQuery获取同级元素的简单代码
2016/07/09 Javascript
详解webpack异步加载业务模块
2017/06/23 Javascript
详解vue2.0 不同屏幕适配及px与rem转换问题
2018/02/23 Javascript
使用Node.js实现一个多人游戏服务器引擎
2019/03/13 Javascript
layui递归实现动态左侧菜单
2019/07/26 Javascript
关于vue利用postcss-pxtorem进行移动端适配的问题
2019/11/20 Javascript
Vue.extend 登录注册模态框的实现
2020/12/29 Vue.js
Python 连连看连接算法
2008/11/22 Python
一键搞定python连接mysql驱动有关问题(windows版本)
2016/04/23 Python
实例讲解Python中SocketServer模块处理网络请求的用法
2016/06/28 Python
Python序列操作之进阶篇
2016/12/08 Python
python itchat实现微信好友头像拼接图的示例代码
2017/08/14 Python
python2.7安装图文教程
2018/03/13 Python
python 判断linux进程,并杀死进程的实现方法
2019/07/01 Python
Python 实现网课实时监控自动签到、打卡功能
2020/03/12 Python
Sneaker Studio匈牙利:购买运动鞋
2018/03/26 全球购物
职业技术学校毕业生推荐信
2013/12/03 职场文书
自荐信范文
2013/12/10 职场文书
大学生自我鉴定范文
2013/12/28 职场文书
《三个小伙伴》教学反思
2014/04/11 职场文书
小人国观后感
2015/06/11 职场文书
课题研究阶段性总结
2015/08/13 职场文书
初三数学教学反思
2016/02/17 职场文书
工作一年自我鉴定
2019/06/20 职场文书
css布局巧妙技巧之css三角示例的运用
2022/03/16 HTML / CSS