国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

Spring Boot/VUE中路由傳遞參數(shù)的實(shí)現(xiàn)代碼

2024-05-06 15:21:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在路由時(shí)傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根據(jù)代碼看一下,VUE 和 Spring Boot 中各自是如何處理傳遞和接受參數(shù)的。

Spring Bootpackage 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){  // 訪問(wèn) http://localhost:8080/router/tang/101?type=spor&num=12  return name + classid + type + num;  } } 在url路徑中的,被稱為pathVariable,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。VUEroutes: [  {  path: '/',  name: 'HomePage',  component: HomePage  },  {  path: '/user/:id?/:type?',  name: 'User',  component: User  }  ] 

首先在路由中配置url中需要傳遞的參數(shù),被稱為動(dòng)態(tài)路徑參數(shù)。以“:”開(kāi)始,末尾的“?”表示為可選的參數(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為動(dòng)態(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實(shí)例中獲取,動(dòng)態(tài)路徑參數(shù)在params里,查詢參數(shù)在query里。

當(dāng)vue的動(dòng)態(tài)路徑組件處在激活狀態(tài)時(shí),如果改變動(dòng)態(tài)路徑參數(shù),那么寫(xiě)在created()的方法將不會(huì)再次被調(diào)用,因?yàn)樵摻M件已經(jīng)創(chuàng)建好了。此時(shí),可以為$route添加一個(gè)watch,當(dāng)其發(fā)生變化時(shí),再獲取數(shù)據(jù)。

在路由時(shí)傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根據(jù)代碼看一下,VUE 和 Spring Boot 中各自是如何處理傳遞和接受參數(shù)的。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 神池县| 富平县| 凌海市| 万荣县| 靖西县| 古交市| 崇仁县| 高唐县| 辽阳县| 鹤壁市| 陇川县| 万年县| 镇康县| 泰州市| 鄂托克旗| 阳高县| 教育| 宿迁市| 兰考县| 应用必备| 云梦县| 海淀区| 浦北县| 衡东县| 伊宁县| 永善县| 霍邱县| 松溪县| 巴林右旗| 丹凤县| 抚州市| 临朐县| 济南市| 江都市| 武强县| 阜南县| 荆门市| 湖南省| 山丹县| 花莲县| 兴和县|