今天就跟大家聊聊有關(guān)vue-router中參數(shù)傳遞的方式有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
秭歸ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!vue-router傳遞參數(shù)分為兩大類
編程式的導(dǎo)航 router.push
聲明式的導(dǎo)航 <router-link>
編程式的導(dǎo)航 router.push
編程式導(dǎo)航傳遞參數(shù)有兩種類型:字符串、對象。
字符串
字符串的方式是直接將路由地址以字符串的方式來跳轉(zhuǎn),這種方式很簡單但是不能傳遞參數(shù):
this.$router.push("home");
對象
想要傳遞參數(shù)主要就是以對象的方式來寫,分為兩種方式:命名路由、查詢參數(shù),下面分別說明兩種方式的用法和注意事項(xiàng)。
命名路由
命名路由的前提就是在注冊路由的地方需要給路由命名如:
命名路由傳遞參數(shù)需要使用params來傳遞,這里一定要注意使用params不是query。
目標(biāo)頁面接收傳遞參數(shù)時(shí)使用params
特別注意:命名路由這種方式傳遞的參數(shù),如果在目標(biāo)頁面刷新是會出錯的
使用方法如下:
this.$router.push({ name: 'news', params: { userId: 123 }})
代碼如下:
<template> <div class="hello"> <h2>{{ msg }}</h2> <button @click="routerTo">click here to news page</button> </div> </template> <script> export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App' } }, methods:{ routerTo(){ this.$router.push({ name: 'news', params: { userId: 123 }}); } } } </script> <style> </style>接受傳遞的參數(shù):<template> <div> this is the news page.the transform param is {{this.$route.params.userId}} </div> </template> <script> </script>
運(yùn)行效果如下:
查詢參數(shù)
查詢參數(shù)其實(shí)就是在路由地址后面帶上參數(shù)和傳統(tǒng)的url參數(shù)一致的,傳遞參數(shù)使用query而且必須配合path來傳遞參數(shù)而不能用name,目標(biāo)頁面接收傳遞的參數(shù)使用query。
注意:和name配對的是params,和path配對的是query
使用方法如下:
this.$router.push({ path: '/news', query: { userId: 123 }});代碼如下:<template> <div class="hello"> <h2>{{ msg }}</h2> <button @click="routerTo">click here to news page</button> </div> </template> <script> export default { name: 'HelloWorld', data () { return { msg: 'Welcome to Your Vue.js App' } }, methods:{ routerTo(){ this.$router.push({ path: '/news', query: { userId: 123 }}); } } } </script> <style> </style>接收參數(shù)如下:<template> <div> this is the news page.the transform param is {{this.$route.query.userId}} </div> </template> <script> </script>
運(yùn)行效果如下:
聲明式的導(dǎo)航
聲明式的導(dǎo)航和編程式的一樣,這里就不在過多介紹,給幾個例子大家對照編程式理解,
例子如下:
字符串
<router-link to="news">click to news page</router-link>
命名路由
<router-link :to="{ name: 'news', params: { userId: 1111}}">click to news page</router-link>
運(yùn)行效果如下:
查詢參數(shù)
<router-link :to="{ path: '/news', query: { userId: 1111}}">click to news page</router-link>
運(yùn)行效果如下:
最后總結(jié):
路由傳遞參數(shù)和傳統(tǒng)傳遞參數(shù)是一樣的,命名路由類似表單提交而查詢就是url傳遞,在vue項(xiàng)目中基本上掌握了這兩種傳遞參數(shù)就能應(yīng)付大部分應(yīng)用了,最后總結(jié)為以下兩點(diǎn):
1.命名路由搭配params,刷新頁面參數(shù)會丟失
2.查詢參數(shù)搭配query,刷新頁面數(shù)據(jù)不會丟失
3.接受參數(shù)使用this.$router后面就是搭配路由的名稱就能獲取到參數(shù)的值
看完上述內(nèi)容,你們對vue-router中參數(shù)傳遞的方式有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享文章:vue-router中參數(shù)傳遞的方式有哪些-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article20/ccpico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站改版、企業(yè)建站、建站公司、企業(yè)網(wǎng)站制作、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容