Vue頁(yè)面路由參數(shù)的傳遞和獲取方式
vue頁(yè)面路由切換時(shí)傳參的方式
有如下幾種:
1.動(dòng)態(tài)路由參數(shù)
- 它隱藏字段信息,相對(duì)于來(lái)說(shuō)較安全,同時(shí)地址欄中的地址也相對(duì)較短
- 它必須是先定義后使用,一般用于根據(jù)固定參數(shù),返回對(duì)應(yīng)的數(shù)據(jù)所用
2.query字符串 ?id=1
- 通過(guò)search字符串的方式來(lái)在地址欄中傳遞數(shù)據(jù),相對(duì)于來(lái)說(shuō)地址欄會(huì)暴露字段信息安全性較低,
- 一般用于搜索相關(guān),它可以不定義就可以直接用
3.props 隱式傳遞
- 隱式傳遞,它一般用于敏感數(shù)據(jù)的傳遞,可以不用
4.cookie/storage來(lái)完成對(duì)于頁(yè)面?zhèn)鲄?/p>
1. 通過(guò)動(dòng)態(tài)路由參數(shù)傳遞
描述:
- 當(dāng)我們確定了某一個(gè)頁(yè)面需要根據(jù)唯一標(biāo)識(shí)來(lái)獲取詳情的時(shí)候,我們一般使用動(dòng)態(tài)路由傳遞參數(shù)。
- 要注意,通過(guò)這種方式傳遞數(shù)據(jù),動(dòng)態(tài)路由必須先定義后使用,并且獲取數(shù)據(jù)時(shí)需要唯一標(biāo)識(shí)。
使用:
news.js(這個(gè)文件是從 index.js 文件中抽取拆分出來(lái)的,最終要被引入到 insex.js 文件中):
import News from '@/views/News'
import Detail from '@/views/Detail'
const routes = [
{
path: '/news',
component: News,
},
{
// 通過(guò)動(dòng)態(tài)路由參數(shù)來(lái)完成頁(yè)面數(shù)據(jù)的傳遞
path: '/news/:id',
component: Detail,
},
]
export default routes
index.js:
import Vue from 'vue'
import VueRouter from 'vue-router'
import news from './routes/news'
// 以插件的方式添加
Vue.use(VueRouter)
// 實(shí)例化路由對(duì)象及配置路由表
const routes = [...news]
const router = new VueRouter({
// 路由模式
mode: 'history',
// 路由規(guī)則表
routes
})
export default router
new.json(虛擬新聞 mooc 數(shù)據(jù)):
[
{ "id": 1000, "title": "新聞1" },
{ "id": 2000, "title": "新聞2" },
{ "id": 3000, "title": "新聞3" }
]
new.vue(新聞頁(yè)):
<template>
<div>
<ul>
<template v-if="news == null">
<li>加載中...</li>
</template>
<template v-else>
<li @click="godetail(item.id)" v-for="item in news" :key="item.id">{{ item.title }}</li>
</template>
</ul>
</div>
</template>
<script>
import { get } from '@/utils/http'
export default {
data() {
return {
news: null
}
},
async created() {
let ret = await get('/mock/news.json')
this.news = ret
},
methods: {
godetail(id) {
this.$router.push(`/news/${id}`)
}
}
}
</script>
<style lang="scss" scoped></style>
detail.vue(新聞詳情頁(yè)):
<template>
<div>
</div>
</template>
<script>
export default {
mounted() {
// 獲取動(dòng)態(tài)路由參數(shù)數(shù)據(jù)
console.log(this.$route.params)
}
}
</script>
<style lang="scss" scoped></style>

2. 通過(guò)query字符串傳遞
new.vue(新聞頁(yè)):
<template>
<div>
<ul>
<template v-if="news == null">
<li>加載中...</li>
</template>
<template v-else>
<li @click="godetail(item.id)" v-for="item in news" :key="item.id">{{ item.title }}</li>
</template>
</ul>
</div>
</template>
<script>
import { get } from '@/utils/http'
export default {
data() {
return {
news: null
}
},
async created() {
let ret = await get('/mock/news.json')
this.news = ret
},
methods: {
godetail(id) {
this.$router.push(`/news/${id}?kw=abc`)
}
}
}
</script>
<style lang="scss" scoped></style>
detail.vue(新聞詳情頁(yè)):
<template>
<div>
</div>
</template>
<script>
export default {
mounted() {
// 獲取query字符串
console.log(this.$route.query)
}
}
</script>
<style lang="scss" scoped></style>

3. props 隱式傳遞
news.js:
import News from '@/views/News'
import Detail from '@/views/Detail'
const routes = [
{
path: '/news',
component: News,
},
{
// 通過(guò)動(dòng)態(tài)路由參數(shù)來(lái)完成頁(yè)面數(shù)據(jù)的傳遞
path: '/news/:id',
component: Detail,
// 寫法1:回調(diào)函數(shù)寫法,可以書寫業(yè)務(wù)邏輯
// router它就是一個(gè)路由對(duì)象
props: router => {
let title = router.params.id == '1000' ? '爆炸新聞' : '一般新聞'
return {
state: 2000,
title
}
},
// 寫法2:這是一種沒什么用的寫法,沒有業(yè)務(wù)邏輯
// props: { title: '1', state: 2 }
}
]
export default routes
new.vue(新聞頁(yè)):
<template>
<div>
<ul>
<template v-if="news == null">
<li>加載中...</li>
</template>
<template v-else>
<li @click="godetail(item.id)" v-for="item in news" :key="item.id">{{ item.title }}</li>
</template>
</ul>
</div>
</template>
<script>
import { get } from '@/utils/http'
export default {
data() {
return {
news: null
}
},
async created() {
let ret = await get('/mock/news.json')
this.news = ret
},
methods: {
godetail(id) {
this.$router.push(`/news/${id}?kw=abc`)
}
}
}
</script>
<style lang="scss" scoped></style>
detail.vue(新聞詳情頁(yè)):
<template>
<div>
<h3>props: {{ state }} -- {{ title }}</h3>
</div>
</template>
<script>
export default {
props: ['state','title'],
}
</script>
<style lang="scss" scoped></style>

這種傳遞方式,可以敏感字段從地址欄中隱藏,不會(huì)暴露數(shù)據(jù),而且回調(diào)函數(shù)的寫法可以書寫業(yè)務(wù)邏輯。
這種方式可以做數(shù)據(jù)埋點(diǎn)(也叫用戶指紋),即隱蔽地收集用戶數(shù)據(jù)。用戶每次跳轉(zhuǎn)頁(yè)面都會(huì)觸發(fā) props 隱式傳遞,從而做到用戶數(shù)據(jù)的收集。收集到數(shù)據(jù)后,通過(guò)python、大數(shù)據(jù)等技術(shù),為用戶建模,生成人物畫像,由此可以進(jìn)行大數(shù)據(jù)推薦。
除了上面兩種寫法以外,還有第三種寫法
news.js:
import News from '@/views/News'
import Detail from '@/views/Detail'
const routes = [
{
path: '/news',
component: News,
},
{
// 通過(guò)動(dòng)態(tài)路由參數(shù)來(lái)完成頁(yè)面數(shù)據(jù)的傳遞
path: '/news/:id',
component: Detail,
// 寫法3:布爾類型
// 布爾類型,一但使用,params動(dòng)態(tài)路由參數(shù)傳的數(shù)據(jù),可以通過(guò)props來(lái)獲取
// 設(shè)置為布爾類型,可以簡(jiǎn)化,動(dòng)態(tài)路由參數(shù)的數(shù)據(jù)獲取
props: true,
}
]
export default routes
detail.vue(新聞詳情頁(yè)):
<template>
<div>
<!-- 直接通過(guò) props 獲取動(dòng)態(tài)路由參數(shù) -->
<h3>props: {{ $route.params }} -- {{ id }}</h3>
</div>
</template>
<script>
export default {
props: ["id"],
};
</script>
<style lang="scss" scoped></style>

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue高德地圖JS API實(shí)現(xiàn)海量點(diǎn)標(biāo)記示例
本文主要介紹了vue高德地圖JS API實(shí)現(xiàn)海量點(diǎn)標(biāo)記示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在vant中使用時(shí)間選擇器和popup彈出層的操作
這篇文章主要介紹了在vant中使用時(shí)間選擇器和popup彈出層的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
淺談Vuejs中nextTick()異步更新隊(duì)列源碼解析
本篇文章主要介紹了淺談Vuejs中nextTick()異步更新隊(duì)列源碼解析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
vue表格n-form中自定義增加必填星號(hào)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue表格n-form中自定義增加必填星號(hào),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
Vue清除定時(shí)器setInterval優(yōu)化方案分享
這篇文章主要介紹了Vue清除定時(shí)器setInterval優(yōu)化方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

