Spring Boot/VUE中路由傳遞參數的實現代碼,springvue

來源:互聯網
上載者:User

Spring Boot/VUE中路由傳遞參數的實現代碼,springvue

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

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){  // 訪問 http://localhost:8080/router/tang/101?type=spor&num=12  return name + classid + type + num;  } } 在url路徑中的,被稱為pathVariable,查詢參數被稱為pequestParm。在controller中接受參數,可以直接在方法裡用了。VUEroutes: [  {  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 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){  // 訪問 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 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){  // 訪問 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中路由傳遞參數的實現代碼,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.