欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年03月02日 08:35:29   作者:網(wǎng)路惡霸  
在路由時傳遞參數(shù),一般有兩種形式,一種是拼接在url地址中,另一種是查詢參數(shù)。這篇文章主要介紹了Spring Boot/VUE中路由傳遞參數(shù),需要的朋友可以參考下

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

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,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

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

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

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

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

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,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

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

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

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

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

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,查詢參數(shù)被稱為pequestParm。在controller中接受參數(shù),可以直接在方法里用了。

VUE

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

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

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

總結(jié)

以上所述是小編給大家介紹的Spring Boot/VUE中路由傳遞參數(shù)的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vuex中Store的簡單實現(xiàn)

    Vuex中Store的簡單實現(xiàn)

    這篇文章主要介紹了Vuex中Store的簡單實現(xiàn),為了在 Vue 組件中訪問 this.$store property,你需要為 Vue 實例提供創(chuàng)建好的 store,Vuex 提供了一個從根組件向所有子組件,以 store 選項的方式 注入 該 store 的機制,需要的朋友可以參考下
    2023-11-11
  • Vue?UI創(chuàng)建項目詳細步驟

    Vue?UI創(chuàng)建項目詳細步驟

    本文主要介紹了Vue?UI創(chuàng)建項目詳細步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • element-plus結(jié)合sortablejs實現(xiàn)table行拖拽效果

    element-plus結(jié)合sortablejs實現(xiàn)table行拖拽效果

    使用element-plus的el-table組件創(chuàng)建出來的table,結(jié)合sortable.js實現(xiàn)table行拖動排序,文中有詳細的代碼示例供大家參考,具有一定的參考價值,感興趣的同學(xué)可以自己動手試一試
    2023-10-10
  • Vue+Echarts實現(xiàn)簡單折線圖

    Vue+Echarts實現(xiàn)簡單折線圖

    這篇文章主要為大家詳細介紹了Vue+Echarts實現(xiàn)簡單折線圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié)(推薦)

    頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié)(推薦)

    這篇文章主要介紹了頁面內(nèi)錨點定位及跳轉(zhuǎn)方法總結(jié),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue-cli3 打包優(yōu)化之 splitchunks詳解

    vue-cli3 打包優(yōu)化之 splitchunks詳解

    這篇文章主要介紹了vue-cli3 打包優(yōu)化之 splitchunks的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 拿來就用vue-gird-layout組件封裝示例

    拿來就用vue-gird-layout組件封裝示例

    這篇文章主要介紹了vue-gird-layout組件封裝示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • vue點擊按鈕跳轉(zhuǎn)到另一個vue頁面實現(xiàn)方法

    vue點擊按鈕跳轉(zhuǎn)到另一個vue頁面實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于vue點擊按鈕跳轉(zhuǎn)到另一個vue頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Vue2利用Axios發(fā)起請求的詳細過程記錄

    Vue2利用Axios發(fā)起請求的詳細過程記錄

    有很多時候你在構(gòu)建應(yīng)用時需要訪問一個API并展示其數(shù)據(jù),做這件事的方法有好幾種,而使用基于promise的HTTP客戶端axios則是其中非常流行的一種,這篇文章主要給大家介紹了關(guān)于Vue2利用Axios發(fā)起請求的詳細過程,需要的朋友可以參考下
    2021-12-12
  • 使用vue實現(xiàn)玉兔迎春圖高亮示例詳解

    使用vue實現(xiàn)玉兔迎春圖高亮示例詳解

    這篇文章主要為大家介紹了使用vue實現(xiàn)玉兔迎春圖高亮示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01

最新評論