国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Vue-Router】路由傳參

這篇具有很好參考價值的文章主要介紹了【Vue-Router】路由傳參。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

1. query 傳參

【Vue-Router】路由傳參,Vue-Router,vue.js,javascript,前端
list.json

{
  "data": [
    {
      "name": "面",
      "price":300,
      "id": 1
    },
    {
      "name": "水",
      "price":400,
      "id": 2
    },
    {
      "name": "菜",
      "price":500,
      "id": 3
    }
  ]

}

login.vue

<template>
  <h1>
    我是列表頁面
  </h1>
  <table cellpadding="0" class="table" border="1">
    <thead>
      <tr>
        <th>商品</th>
        <th>價格</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr :key="item.id" v-for="item in data">
        <th>{{ item.name }}</th>
        <th>{{ item.price }}</th>
        <th>
          <button @click="toDetail(item)">詳情</button>
        </th>
      </tr>
    </tbody>
  </table>
</template>

<script setup lang="ts">
import { data } from './list.json'
import { useRouter } from 'vue-router';

const router = useRouter()

type Item = {
  name: string;
  price: number;
  id: number;
}

const toDetail = (item: Item) => {
  router.push({
    path: '/reg',
    query: {
      id: item.id,
      name: item.name,
      price: item.price
    }
  })
}
</script>

<style scoped>
.table {
  width: 400px;
}
</style>

reg.vue

<template>
  <h1>
    我是列表頁面
  </h1>
  <button @click="router.back">返回</button>
  <div style="font-size: 20px;">
    品牌:{{ route.query.name }}
  </div>
    <div style="font-size: 20px;">
    價格:{{ route.query.price }}
  </div>
    <div style="font-size: 20px;">
    id: {{ route.query.id }}
  </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';

const router = useRouter();
const route = useRoute();

</script>

<style scoped>
.reg {
  background-color: green;
  height: 400px;
  width: 400px;
  font-size: 20px;
  color: white;
}
</style>

App.vue

<template>
  <h1>hello world</h1>
 
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();

</script>

<style scoped></style>

【Vue-Router】路由傳參,Vue-Router,vue.js,javascript,前端
【Vue-Router】路由傳參,Vue-Router,vue.js,javascript,前端

2. 動態(tài)路由參數(shù)(params參數(shù))

index.ts

import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from "vue-router";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: 'Login',
    component: () => import("../components/login.vue")
  },
  {
    path: "/reg/:id",
    name: 'Reg',
    component: () => import("../components/reg.vue")
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

reg.vue

<template>
  <h1>
    我是列表頁面
  </h1>
  <button @click="router.back()">返回</button>
  <div style="font-size: 20px;">
    品牌:{{ item?.name }}
  </div>
  <div style="font-size: 20px;">
    價格:{{ item?.price }}
  </div>
  <div style="font-size: 20px;">
    id: {{ item?.id }}
  </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import { data } from './list.json';

const router = useRouter();
const route = useRoute();
// 返回對象用item接收
const item = data.find(v => v.id === Number(route.params.id))


</script>

<style scoped>
.reg {
  background-color: green;
  height: 400px;
  width: 400px;
  font-size: 20px;
  color: white;
}
</style>

item?.name ,item?.price ,item?.id,他們?nèi)绻皇褂每蛇x鏈操作符會出現(xiàn)報錯:'__VLS_ctx.item' is possibly 'undefined'.

login.vue

<template>
  <h1>
    我是列表頁面
  </h1>
  <table cellpadding="0" class="table" border="1">
    <thead>
      <tr>
        <th>商品</th>
        <th>價格</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr :key="item.id" v-for="item in data">
        <th>{{ item.name }}</th>
        <th>{{ item.price }}</th>
        <th>
          <button @click="toDetail(item)">詳情</button>
        </th>
      </tr>
    </tbody>
  </table>
</template>

<script setup lang="ts">
import { data } from './list.json'
import { useRouter } from 'vue-router';

const router = useRouter()

type Item = {
  name: string;
  price: number;
  id: number;
}

const toDetail = (item: Item) => {
  router.push({
    // name 對應(yīng) router 的 name
    name: 'Reg',
    // 不會展示在URL上,存在于內(nèi)存里
    params: {
      id: item.id
    }
  })
}
</script>

<style scoped>
.table {
  width: 400px;
}
</style>

【Vue-Router】路由傳參,Vue-Router,vue.js,javascript,前端

注意:

  • 傳遞params參數(shù)時,若使用to的對象寫法,必須使用name配置項(xiàng),不能用path
  • 傳遞params參數(shù)時,需要提前在規(guī)則中占位。

2.1 路由的 props 配置

將路由參數(shù)作為props傳給組件。文章來源地址http://www.zghlxwxcb.cn/news/detail-647089.html

{
	name:'xiang',
	path:'detail/:id/:title/:content',
	component:Detail,

  // props的對象寫法,作用:把對象中的每一組key-value作為props傳給Detail組件。但這種寫法把數(shù)據(jù)寫死了,不推薦
  // props:{a:1,b:2,c:3}, 

  // props的布爾值寫法,作用:把收到了每一組params參數(shù),作為props傳給Detail組件,用defineProps接收,直接使用參數(shù)
    props:true
  
  // props的函數(shù)寫法,作用:把返回的對象中每一組key-value作為props傳給Detail組件,用defineProps接收,直接使用參數(shù)
  //props(route){
    //return route.query
  //}
}
<template>
  <ul>
    <li>{{ id }}</li>
    <li>{{ title }}</li>
    <li>{{ content }}</li>
  </ul>
</template>
<script setup lang="ts">
  defineProps(['id', 'title', 'content'])
</script>

到了這里,關(guān)于【Vue-Router】路由傳參的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Vue3+Vue-Router+Element-Plus根據(jù)后端數(shù)據(jù)實(shí)現(xiàn)前端動態(tài)路由——權(quán)限管理模塊

    Vue3+Vue-Router+Element-Plus根據(jù)后端數(shù)據(jù)實(shí)現(xiàn)前端動態(tài)路由——權(quán)限管理模塊

    提示:文章內(nèi)容仔細(xì)看一些,或者直接粘貼復(fù)制,效果滿滿 提示:文章大概 1、項(xiàng)目:前后端分離 2、前端:基于Vite創(chuàng)建的Vue3項(xiàng)目 3、后端:沒有,模擬的后端數(shù)據(jù) 4、關(guān)于路徑“@”符號——vite.config.js 文件里修改 提示:以下是本篇文章正文內(nèi)容,下面案例可供復(fù)制粘貼使用

    2024年02月02日
    瀏覽(122)
  • Vue入門六(前端路由的概念與原理|Vue-router簡單使用|登錄跳轉(zhuǎn)案例|scoped樣式|混入(mixin)|插件)

    Vue入門六(前端路由的概念與原理|Vue-router簡單使用|登錄跳轉(zhuǎn)案例|scoped樣式|混入(mixin)|插件)

    路由(英文:router)就是對應(yīng)關(guān)系 SPA指的是一個web網(wǎng)站只有一個唯一的一個HTML頁面, 所有組件的展示與切換 都在唯一的一個頁面內(nèi)完成。 此時, 不同組件之間的切換 需要通過 前端路由 來實(shí)現(xiàn) 總結(jié):在SPA項(xiàng)目中, 不同功能之間的切換 ,要 依賴于前端路由 來完成 通俗移動

    2024年01月22日
    瀏覽(29)
  • 路由vue-router

    路由vue-router

    路由(英文:router)就是 對應(yīng)關(guān)系 SPA 指的是一個 web 網(wǎng)站只有 唯一的一個 HTML 頁面 ,所有組件的展示與切換都在這唯一的一個頁面內(nèi)完成。 此時,不同組件之間的切換需要通過前端路由來實(shí)現(xiàn)。 結(jié)論: 在 SPA 項(xiàng)目中,不同功能之間的切換 ,要依賴于 前端路由 來完成!

    2024年02月07日
    瀏覽(25)
  • vue-router路由守衛(wèi)

    在我們使用vue-router的時候,路由守衛(wèi)就像監(jiān)聽器、攔截器一樣,幫我們做一些鑒權(quán)等操作,vue中的路由守衛(wèi)分為全局路由守衛(wèi)、獨(dú)享路由守衛(wèi)、組件內(nèi)的路由守衛(wèi) 全局路由守衛(wèi) :?beforeEach、 afterEach 組件獨(dú)享路由守衛(wèi) :beforeEnter、 beforeLeave 組件內(nèi)路由守衛(wèi) :beforeRouteEnter、

    2024年02月11日
    瀏覽(16)
  • 【Vue-Router】路由入門

    【Vue-Router】路由入門

    路由(Routing)是指確定網(wǎng)站或應(yīng)用程序中特定頁面的方式。在Web開發(fā)中,路由用于根據(jù)URL的不同部分來確定應(yīng)用程序中應(yīng)該顯示哪個內(nèi)容。 構(gòu)建前端項(xiàng)目 安裝依賴和路由 3. router 使用 login.vue reg.vue index.ts App.vue main.ts router-view 補(bǔ)充: router-view 是Vue Router提供的一個組件,用于

    2024年02月13日
    瀏覽(28)
  • Vue3配置路由(vue-router)

    Vue3配置路由(vue-router)

    緊接上篇文章,vue3的配置與vue2是有所差別的,本文就講述了如何配置,如果本文對你有所幫助請三連支持博主。 下面案例可供參考 使用npm命令進(jìn)行安裝 : npm install vue-router@4 完成后我們打開項(xiàng)目根目錄下的 package.json 文件: 如下即為成功 這里創(chuàng)建 view目錄,然后在view目錄

    2023年04月12日
    瀏覽(27)
  • vue3使用vue-router嵌套路由(多級路由)

    vue3使用vue-router嵌套路由(多級路由)

    Vue3 嵌套路由的使用和 Vue2 相差不大,主要的區(qū)別是 Vue3 的路由實(shí)例化使用了 createApp() 方法,所以實(shí)例化路由時需要傳入根組件。另外,Vue3 的路由對象除了包含 Vue2 中的導(dǎo)航守衛(wèi)、導(dǎo)航鉤子和解析守衛(wèi)等功能外,還新增了 meta prop 和 route prop。 在使用嵌套路由時,建議將路由

    2024年02月03日
    瀏覽(29)
  • vue-router路由懶加載

    vue-router路由懶加載

    路由懶加載指的是打包部署時將資源按照對應(yīng)的頁面進(jìn)行劃分,需要的時候加載對應(yīng)的頁面資源,而不是把所有的頁面資源打包部署到一塊。避免不必要資源加載。(參考vue項(xiàng)目實(shí)現(xiàn)路由按需加載(路由懶加載)的3種方式_小胖梅的博客-CSDN博客_vue懶加載?) 這里有三種方式可以

    2023年04月08日
    瀏覽(20)
  • vue-router(路由)詳細(xì)教程

    路由是一個比較廣義和抽象的概念,路由的本質(zhì)就是 對應(yīng)關(guān)系 。 在開發(fā)中,路由分為: ? 后端路由 ? 前端路由 后端路由 概念:根據(jù)不同的用戶 URL 請求,返回不同的內(nèi)容 本質(zhì):URL 請求地址與服務(wù)器資源之間的對應(yīng)關(guān)系 [外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將

    2024年02月04日
    瀏覽(21)
  • vue-router路由模式詳解

    vue-router路由模式詳解

    目錄 一. vue-router(前端路由)有兩種模式,hash模式和history模式 二、路由模式解析 三、兩種模式的區(qū)別 1、hash模式 ?2、history路由 (3)popstate實(shí)現(xiàn)history路由攔截,監(jiān)聽頁面返回事件 一. vue-router(前端路由)有兩種模式,hash模式和history模式 1.hash 就是指 url 后面的 # 號以及后

    2024年02月03日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包