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 相关文章推荐
Sample script that deletes a SQL Server database
Jun 16 Javascript
dwr spring的集成实现代码
Mar 22 Javascript
JavaScript实现将xml转换成html table表格的方法
Apr 17 Javascript
js判断价格,必须为数字且不能为负数的实现方法
Oct 07 Javascript
微信公众号  提示:Unauthorized API function 问题解决方法
Dec 05 Javascript
jQuery插件FusionCharts实现的MSBar3D图效果示例【附demo源码】
Mar 23 jQuery
利用node.js本地搭建HTTP服务器
Apr 19 Javascript
JS实现按钮颜色切换效果
Sep 05 Javascript
jquery实现侧边栏左右伸缩效果的示例
Dec 19 jQuery
Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
Apr 26 Javascript
swiper Scrollbar滚动条组件详解
Sep 08 Javascript
node实现mock-plugin中间件的方法
Dec 25 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数组是否为空的代码
2011/09/08 PHP
从PHP的源码中深入了解stdClass类
2014/04/18 PHP
PHP静态文件生成类实例
2014/11/29 PHP
PHP中echo与print区别点整理
2021/03/09 PHP
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
jQuery中:focus选择器用法实例
2014/12/30 Javascript
jquery实现页面虚拟键盘特效
2015/08/08 Javascript
require.js配合插件text.js实现最简单的单页应用程序
2016/07/12 Javascript
微信小程序 this和that详解及简单实例
2017/02/13 Javascript
什么是Vue.js框架 为什么选择它?
2017/10/17 Javascript
详解在vue-cli中引用jQuery、bootstrap以及使用sass、less编写css
2017/11/08 jQuery
原生js实现简单的焦点图效果实例
2017/12/14 Javascript
node.js连接mysql与基本用法示例
2019/01/05 Javascript
javascript数组去重方法总结(推荐)
2019/03/20 Javascript
Node.js 在本地生成日志文件的方法
2020/02/07 Javascript
linux 下以二进制的方式安装 nodejs
2020/02/12 NodeJs
Vue中nprogress页面加载进度条的方法实现
2020/11/13 Javascript
pycharm 使用心得(八)如何调用另一文件中的函数
2014/06/06 Python
用python删除java文件头上版权信息的方法
2014/07/31 Python
Python中的类与对象之描述符详解
2015/03/27 Python
使用Python编写基于DHT协议的BT资源爬虫
2016/03/19 Python
Django 使用Ajax进行前后台交互的示例讲解
2018/05/28 Python
对Python正则匹配IP、Url、Mail的方法详解
2018/12/25 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
Python 3 判断2个字典相同
2019/08/06 Python
python django 原生sql 获取数据的例子
2019/08/14 Python
Python爬取微信小程序Charles实现过程图解
2020/09/29 Python
Orvis官网:自1856年以来,优质服装、飞钓装备等
2018/12/17 全球购物
美国最大和最受信任的二手轮胎商店:Bestusedtires.com
2020/06/02 全球购物
放飞中国梦演讲稿
2014/04/23 职场文书
活动总结怎么写啊
2014/05/07 职场文书
工程项目经理任命书
2014/06/05 职场文书
公司授权委托书范本
2014/09/18 职场文书
四风问题个人自查剖析材料思想汇报
2014/09/21 职场文书
个人租房协议书范本
2014/09/30 职场文书
pandas中DataFrame重置索引的几种方法
2021/05/24 Python