最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue頁(yè)面路由參數(shù)的傳遞和獲取方式

 更新時(shí)間:2026年03月26日 09:26:01   作者:月光曬了很涼快  
這篇文章主要介紹了Vue頁(yè)面路由參數(shù)的傳遞和獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)記示例

    本文主要介紹了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
  • vue集成chart.js的實(shí)現(xiàn)方法

    vue集成chart.js的實(shí)現(xiàn)方法

    這篇文章主要介紹了vue集成chartjs的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 在vant中使用時(shí)間選擇器和popup彈出層的操作

    在vant中使用時(shí)間選擇器和popup彈出層的操作

    這篇文章主要介紹了在vant中使用時(shí)間選擇器和popup彈出層的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • 淺談Vuejs中nextTick()異步更新隊(duì)列源碼解析

    淺談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)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue表格n-form中自定義增加必填星號(hào),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • vue3.0中使用nextTick方式

    vue3.0中使用nextTick方式

    這篇文章主要介紹了vue3.0中使用nextTick方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue中防抖和節(jié)流的使用方法

    vue中防抖和節(jié)流的使用方法

    防抖和節(jié)流是我們?cè)陂_發(fā)過(guò)程中常用優(yōu)化性能的方式,可以減少不必要的計(jì)算,不浪費(fèi)資源,只在適合的時(shí)候再進(jìn)行觸發(fā)計(jì)算,這篇文章主要給大家介紹了關(guān)于vue中防抖和節(jié)流使用的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Vue清除定時(shí)器setInterval優(yōu)化方案分享

    Vue清除定時(shí)器setInterval優(yōu)化方案分享

    這篇文章主要介紹了Vue清除定時(shí)器setInterval優(yōu)化方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 深入淺析Vue.js 中的 v-for 列表渲染指令

    深入淺析Vue.js 中的 v-for 列表渲染指令

    當(dāng)遍歷一個(gè)數(shù)組或枚舉一個(gè)對(duì)象進(jìn)行迭代循環(huán)展示時(shí),就會(huì)用到列表渲染指令 v-for。這篇文章主要介紹了Vue.js 中的 v-for 列表渲染指令,需要的朋友可以參考下
    2018-11-11
  • vue封裝公共方法的實(shí)現(xiàn)代碼

    vue封裝公共方法的實(shí)現(xiàn)代碼

    這篇文章給大家介紹了vue封裝公共方法的實(shí)現(xiàn),文章中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01

最新評(píng)論

兴山县| 田阳县| 阜南县| 丁青县| 洛扎县| 廉江市| 富阳市| 洛浦县| 驻马店市| 松阳县| 湟源县| 大田县| 敖汉旗| 巴南区| 禄劝| 邵阳市| 张家港市| 阿尔山市| 威海市| 射洪县| 曲靖市| 凯里市| 临邑县| 赞皇县| 苗栗市| 吉安县| 连山| 广安市| 南召县| 镇宁| 丰台区| 昌都县| 讷河市| 龙州县| 陆川县| 平定县| 潞城市| 奇台县| 临澧县| 苏尼特左旗| 芮城县|