Vue通過路由實現(xiàn)頁面間參數(shù)的傳遞
在Vue項目開發(fā)中,頁面之間傳遞參數(shù)有很多種方法
1.路由傳遞
// params單個參數(shù)傳遞
// 設(shè)置參數(shù) catid
this.$router.push({
name: '/article/category',
params: {catid: '10'}
})
// 獲取參數(shù) catid
this.$route.params.catid
// 多個參數(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多個參數(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.password2.sessionStorage/localStorage緩存的形式進行傳遞
// 設(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(會話存儲):生命周期是在僅在當前會話下有效。及只要這個瀏覽器窗口沒有關(guān)閉,即使刷新頁面或者進入同源另一個頁面,數(shù)據(jù)依然存在。但是sessionStorage在關(guān)閉了瀏覽器窗口后就會被銷毀。
localStorage(本地存儲):生命周期是永久的,關(guān)閉頁面或瀏覽器之后localStorage中的數(shù)據(jù)也不會消失。localStorage除非主動刪除數(shù)據(jù),否則數(shù)據(jù)永遠不會消失。不可跨瀏覽器共用。
3.父子組件之間的傳值
- 父組件給子組件傳值(通過props屬性)
- 子組件給父組件傳值(通過emit事件)
4.使用vuex進行傳值
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)對象。
store.commit("setAppkey",response.data.HttpData.data.appkey);
store.commit("setInfokey",response.data.HttpData.data.infokey);
console.log(store.state.appkey,store.state.infokey);這里我們介紹通過路由實現(xiàn)頁面間參數(shù)的傳遞
通過路由傳遞參數(shù)有兩個缺點:
1.參數(shù)直接暴露,安全性不高;
2.參數(shù)過長時,影響地址欄美觀。
但是路由傳遞參數(shù)這種方便比較直觀,用起來也方便,可以直接把鏈接地址放到a標簽的href中,也可以作為函數(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
- 由于動態(tài)路由也是傳遞params的,所以在 this.$router.push() 方法中path不能和params一起使用,否則params將無效,接收參數(shù)頁面會是undefined,需要用name來指定頁面,也就是說,用query對應(yīng)path(填寫路徑 /home),用params對應(yīng)name(寫路由js中對應(yīng)的name,而不是路徑)
- 接收數(shù)據(jù)必須在頁面加載完成后,也就是在mounted接收,而不是created。
query和params的區(qū)別:
- query傳值在瀏覽器地址欄傳遞數(shù)據(jù)可見,而params不可見
- query配合著push的path,params配合著push的name進行地址的跳轉(zhuǎn)
實例1:直接在元素中添加跳轉(zhuǎn)地址和參數(shù)
第一步:在router里面的index.js文件中設(shè)置category路由路徑和參數(shù)

第二步:在header.vue里,可以使用a的href直接添加跳轉(zhuǎn)地址和參數(shù)

第三步:在category.vue中接收參數(shù)

實例2:通過路由函數(shù)實現(xiàn)頁面跳轉(zhuǎn)和params參數(shù)傳遞
第一步:在router里面的index.js文件中設(shè)置category路由路徑和參數(shù)

第二步: 在header.vue里,通過@click方法實現(xiàn)push函數(shù)調(diào)用

第三步:methods中編寫push調(diào)用函數(shù)

第四步:在category.vue中接收參數(shù)

實例3:通過路由函數(shù)實現(xiàn)頁面跳轉(zhuǎn)和query參數(shù)傳遞
第一步:在router里面的index.js文件中設(shè)置category路由路徑,但不設(shè)置參數(shù)

第二步: 在header.vue里,通過@click方法實現(xiàn)push函數(shù)調(diào)用

第三步:methods中編寫push調(diào)用函數(shù)

第四步:在category.vue中接收參數(shù)

到此這篇關(guān)于Vue通過路由實現(xiàn)頁面間參數(shù)的傳遞的文章就介紹到這了,更多相關(guān)vue路由實現(xiàn)頁面參數(shù)傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue線上部署請求接口報錯net::ERR_CONNECTION_REFUSED
vue線上部署請求接口報錯net::ERR_CONNECTION_REFUSED問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐
本文主要介紹了vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
vue2.0實現(xiàn)倒計時的插件(時間戳 刷新 跳轉(zhuǎn) 都不影響)
我發(fā)現(xiàn)好多倒計時的插件,刷新都會變成從頭再來,于是自己用vue2.0寫了一個,感覺還不錯,特此分享到腳本之家平臺供大家參考下2017-03-03
Vue3如何自定義v-permission權(quán)限指令
這篇文章主要為大家詳細介紹了Vue3如何編寫一個?v-permission?指令來根據(jù)用戶權(quán)限動態(tài)控制元素的渲染,感興趣的小伙伴可以參考一下2024-12-12
vue深度監(jiān)聽(監(jiān)聽對象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實例
這篇文章主要介紹了vue深度監(jiān)聽(監(jiān)聽對象和數(shù)組的改變)與立即執(zhí)行監(jiān)聽實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

