Vue通過路由實(shí)現(xiàn)頁面間參數(shù)的傳遞
在Vue項(xiàng)目開發(fā)中,頁面之間傳遞參數(shù)有很多種方法
1.路由傳遞
// params單個(gè)參數(shù)傳遞 // 設(shè)置參數(shù) catid this.$router.push({ name: '/article/category', params: {catid: '10'} }) // 獲取參數(shù) catid this.$route.params.catid // 多個(gè)參數(shù)傳遞 // 設(shè)置參數(shù) catid this.$router.push({ name: '/article/category', params: { catid: '10', pcatid: '1', user: { username: 'admin', password: '1234556' } }) // 獲取參數(shù) catid this.$route.params.catid this.$route.params.pcatid this.$route.params.user.username this.$route.params.user.password this.$router.push({ path: '/article/category', params: {catid: '10'} }) // 獲取參數(shù) catid this.$route.params.catid // query多個(gè)參數(shù)傳遞 // 設(shè)置參數(shù) catid this.$router.push({ path: '/article/category', query: { catid: '10', pcatid: '1', user: { username: 'admin', password: '1234556' } }) // 獲取參數(shù) catid this.$route.query.catid this.$route.query.pcatid this.$route.query.user.username this.$route.query.user.password
2.sessionStorage/localStorage緩存的形式進(jìn)行傳遞
// 設(shè)置catid參數(shù) window.localStorage.setItem('catid','10') // 獲取catid參數(shù) window.localStorage.getItem('catid') // 設(shè)置catid參數(shù) window.sessionStorage.setItem('catid','10') // 獲取catid參數(shù) window.sessionStorage.getItem('catid')
注:
sessionStorage(會(huì)話存儲(chǔ)):生命周期是在僅在當(dāng)前會(huì)話下有效。及只要這個(gè)瀏覽器窗口沒有關(guān)閉,即使刷新頁面或者進(jìn)入同源另一個(gè)頁面,數(shù)據(jù)依然存在。但是sessionStorage在關(guān)閉了瀏覽器窗口后就會(huì)被銷毀。
localStorage(本地存儲(chǔ)):生命周期是永久的,關(guān)閉頁面或?yàn)g覽器之后localStorage中的數(shù)據(jù)也不會(huì)消失。localStorage除非主動(dòng)刪除數(shù)據(jù),否則數(shù)據(jù)永遠(yuǎn)不會(huì)消失。不可跨瀏覽器共用。
3.父子組件之間的傳值
- 父組件給子組件傳值(通過props屬性)
- 子組件給父組件傳值(通過emit事件)
4.使用vuex進(jìn)行傳值
export default new Vuex.Store({ state: { appkey: '', infokey: '' }, mutations: { setAppkey(state,appkey){ state.appkey = appkey; }, setInfokey(state,infokey){ state.infokey = infokey; } }, actions: {}, modules: {} });
其中 state 中為需要傳遞的值,mutation中為監(jiān)控值變化并修改值的方法,通過store.commit()方法來觸發(fā)狀態(tài)變更以及store.state來獲取狀態(tài)對(duì)象。
store.commit("setAppkey",response.data.HttpData.data.appkey); store.commit("setInfokey",response.data.HttpData.data.infokey); console.log(store.state.appkey,store.state.infokey);
這里我們介紹通過路由實(shí)現(xiàn)頁面間參數(shù)的傳遞
通過路由傳遞參數(shù)有兩個(gè)缺點(diǎn):
1.參數(shù)直接暴露,安全性不高;
2.參數(shù)過長時(shí),影響地址欄美觀。
但是路由傳遞參數(shù)這種方便比較直觀,用起來也方便,可以直接把鏈接地址放到a標(biāo)簽的href中,也可以作為函數(shù)進(jìn)行封裝,實(shí)現(xiàn)頁面跳轉(zhuǎn)和參數(shù)傳遞。
路由傳參的格式是:
// 傳參 this.$router.push({ name:'home', params: { message: "", data: {} } }) // 或 this.$router.push({ path:'/home', query: { message: "", data: {} } }) // 接參 this.$route.params.message this.$route.query.message
注意:
- 傳參是this.$router,接收參數(shù)是this.$route
- 由于動(dòng)態(tài)路由也是傳遞params的,所以在 this.$router.push() 方法中path不能和params一起使用,否則params將無效,接收參數(shù)頁面會(huì)是undefined,需要用name來指定頁面,也就是說,用query對(duì)應(yīng)path(填寫路徑 /home),用params對(duì)應(yīng)name(寫路由js中對(duì)應(yīng)的name,而不是路徑)
- 接收數(shù)據(jù)必須在頁面加載完成后,也就是在mounted接收,而不是created。
query和params的區(qū)別:
- query傳值在瀏覽器地址欄傳遞數(shù)據(jù)可見,而params不可見
- query配合著push的path,params配合著push的name進(jìn)行地址的跳轉(zhuǎn)
實(shí)例1:直接在元素中添加跳轉(zhuǎn)地址和參數(shù)
第一步:在router里面的index.js文件中設(shè)置category路由路徑和參數(shù)
第二步:在header.vue里,可以使用a的href直接添加跳轉(zhuǎn)地址和參數(shù)
第三步:在category.vue中接收參數(shù)
實(shí)例2:通過路由函數(shù)實(shí)現(xiàn)頁面跳轉(zhuǎn)和params參數(shù)傳遞
第一步:在router里面的index.js文件中設(shè)置category路由路徑和參數(shù)
第二步: 在header.vue里,通過@click方法實(shí)現(xiàn)push函數(shù)調(diào)用
第三步:methods中編寫push調(diào)用函數(shù)
第四步:在category.vue中接收參數(shù)
實(shí)例3:通過路由函數(shù)實(shí)現(xiàn)頁面跳轉(zhuǎn)和query參數(shù)傳遞
第一步:在router里面的index.js文件中設(shè)置category路由路徑,但不設(shè)置參數(shù)
第二步: 在header.vue里,通過@click方法實(shí)現(xiàn)push函數(shù)調(diào)用
第三步:methods中編寫push調(diào)用函數(shù)
第四步:在category.vue中接收參數(shù)
到此這篇關(guān)于Vue通過路由實(shí)現(xiàn)頁面間參數(shù)的傳遞的文章就介紹到這了,更多相關(guān)vue路由實(shí)現(xiàn)頁面參數(shù)傳遞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue+Element ui開發(fā)中碰到的IE問題
今天小編就為大家分享一篇解決Vue+Element ui開發(fā)中碰到的IE問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09解決vue項(xiàng)目中遇到 Cannot find module ‘chalk‘ 報(bào)錯(cuò)的問題
這篇文章主要介紹了解決vue項(xiàng)目中遇到 Cannot find module ‘chalk‘ 報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue3+TypeScript報(bào)錯(cuò):無法找到模塊xx的聲明文件問題
這篇文章主要介紹了Vue3+TypeScript報(bào)錯(cuò):無法找到模塊xx的聲明文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11詳解如何使用vue-cli腳手架搭建Vue.js項(xiàng)目
這篇文章主要介紹了詳解如何使用vue-cli腳手架搭建Vue.js項(xiàng)目 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05vue與bootstrap實(shí)現(xiàn)時(shí)間選擇器的示例代碼
本篇文章主要介紹了vue與bootstrap實(shí)現(xiàn)時(shí)間選擇器的示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08