欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue路由參數(shù)的傳遞與獲取方式詳細(xì)介紹

 更新時(shí)間:2022年09月30日 17:03:26   作者:月光曬了很涼快  
顧名思義,vue路由傳參是指嵌套路由時(shí)父路由向子路由傳遞參數(shù),否則操作無效。傳參方式可以劃分為params傳參和query傳參,params傳參又可以分為url中顯示參數(shù)和不顯示參數(shù)兩種方式。具體區(qū)分和使用后續(xù)分析

前言

vue 頁面路由切換時(shí)傳參的方式有如下幾種:

動(dòng)態(tài)路由參數(shù)

  • 它隱藏字段信息,相對(duì)于來說較安全,同時(shí)地址欄中的地址也相對(duì)較短
  • 它必須是先定義后使用,一般用于根據(jù)固定參數(shù),返回對(duì)應(yīng)的數(shù)據(jù)所用

query字符串 ?id=1

通過search字符串的方式來在地址欄中傳遞數(shù)據(jù),相對(duì)于來說地址欄會(huì)暴露字段信息安全性較低,一般用于搜索相關(guān),它可以不定義就可以直接用

props 隱式傳遞

隱式傳遞,它一般用于敏感數(shù)據(jù)的傳遞,可以不用

cookie/storage來完成對(duì)于頁面?zhèn)鲄?/p>

1. 通過動(dòng)態(tài)路由參數(shù)傳遞

描述:

當(dāng)我們確定了某一個(gè)頁面需要根據(jù)唯一標(biāo)識(shí)來獲取詳情的時(shí)候,我們一般使用動(dòng)態(tài)路由傳遞參數(shù)。

要注意,通過這種方式傳遞數(shù)據(jù),動(dòng)態(tài)路由必須先定義后使用,并且獲取數(shù)據(jù)時(shí)需要唯一標(biāo)識(shí)。

使用:

news.js(這個(gè)文件是從 index.js 文件中抽取拆分出來的,最終要被引入到 insex.js 文件中):

import News from '@/views/News'
import Detail from '@/views/Detail'
const routes = [
  {
    path: '/news',
    component: News,
  },
  {
    // 通過動(dòng)態(tài)路由參數(shù)來完成頁面數(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(新聞頁):

<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(新聞詳情頁):

<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. 通過query字符串傳遞

new.vue(新聞頁):

<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(新聞詳情頁):

<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,
  },
  {
    // 通過動(dòng)態(tài)路由參數(shù)來完成頁面數(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(新聞頁):

<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(新聞詳情頁):

<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)頁面都會(huì)觸發(fā) props 隱式傳遞,從而做到用戶數(shù)據(jù)的收集。收集到數(shù)據(jù)后,通過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,
  },
  {
    // 通過動(dòng)態(tài)路由參數(shù)來完成頁面數(shù)據(jù)的傳遞
    path: '/news/:id',
    component: Detail,
    // 寫法3:布爾類型
    // 布爾類型,一但使用,params動(dòng)態(tài)路由參數(shù)傳的數(shù)據(jù),可以通過props來獲取
    // 設(shè)置為布爾類型,可以簡化,動(dòng)態(tài)路由參數(shù)的數(shù)據(jù)獲取
    props: true,
  }
]
export default routes

detail.vue(新聞詳情頁):

<template>
  <div>
    <!-- 直接通過 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>

到此這篇關(guān)于Vue路由參數(shù)的傳遞與獲取方式詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Vue路由傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue使用xlsx和xlsx-style導(dǎo)出表格出現(xiàn)部分樣式缺失的問題解決

    Vue使用xlsx和xlsx-style導(dǎo)出表格出現(xiàn)部分樣式缺失的問題解決

    這篇文章主要為大家詳細(xì)介紹一下Vue使用xlsx-style導(dǎo)出excel時(shí)樣式的設(shè)置,以及出現(xiàn)添加背景色,合并單元格部分樣式缺失問題的解決,需要的可以參考下
    2024-01-01
  • vue.config.js使用代理配置真實(shí)請(qǐng)求url方式

    vue.config.js使用代理配置真實(shí)請(qǐng)求url方式

    這篇文章主要介紹了vue.config.js使用代理配置真實(shí)請(qǐng)求url方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue使用new?Blob()實(shí)現(xiàn)不同類型的文件下載功能

    Vue使用new?Blob()實(shí)現(xiàn)不同類型的文件下載功能

    這篇文章主要給大家介紹了關(guān)于Vue使用new?Blob()實(shí)現(xiàn)不同類型的文件下載功能的相關(guān)資料,在Vue項(xiàng)目中,經(jīng)常用Blob二進(jìn)制進(jìn)行文件下載功能,需要的朋友可以參考下
    2023-07-07
  • Vue--keep-alive使用實(shí)例詳解

    Vue--keep-alive使用實(shí)例詳解

    這篇文章主要介紹了Vue--keep-alive使用實(shí)例詳解,keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染,用示例代碼介紹Vue的keep-alive的用法,需要的朋友可以參考下
    2022-08-08
  • vue3中defineComponent?的作用詳解

    vue3中defineComponent?的作用詳解

    這篇文章主要介紹了vue3中defineComponent?的作用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • Vue3屬性綁定方法解析

    Vue3屬性綁定方法解析

    這篇文章主要介紹了Vue3屬性綁定方法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Unocss(原子化css)?使用及vue3?+?vite?+?ts講解

    Unocss(原子化css)?使用及vue3?+?vite?+?ts講解

    這篇文章主要介紹了Unocss(原子化css)使用vue3?+?vite?+?ts的方法,簡單介紹了Unocss使用及圖標(biāo)庫使用,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • vue動(dòng)態(tài)添加store、路由和國際化配置方式

    vue動(dòng)態(tài)添加store、路由和國際化配置方式

    這篇文章主要介紹了vue動(dòng)態(tài)添加store、路由和國際化配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 在Vue.js中使用TypeScript的方法

    在Vue.js中使用TypeScript的方法

    這篇文章主要介紹了在Vue.js中使用TypeScript的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 15個(gè)Vue技巧,你都知道嗎

    15個(gè)Vue技巧,你都知道嗎

    在使用 Vue 開發(fā)的這幾年里,掌握一些有用的技巧,使用一些更高級(jí)的技術(shù)點(diǎn),總會(huì)有用的,本文就介紹了15個(gè)Vue技巧,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-02-02

最新評(píng)論