国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue傳參一籮筐(頁面、組件)

2024-05-06 15:41:03
字體:
來源:轉載
供稿:網友

Vue頁面、組件之間傳參方式繁多,此處羅列出常用的幾種方式,歡迎審閱補充。

一丶路由傳參

這里的路由傳參以編程式 router.push(...) 為例,聲明式 <router-link :to="..."> 與之類似。此處模擬情景為從 componentsA.vue 頁面跳轉到 componentsB.vue 頁面傳參。首先,路由配置信息如下:

router.js

import Vue from 'vue'import Router from 'vue-router'import componentsA from './components/componentsA' //在components下創建componentsA.vueimport componentsB from './components/componentsB' //在components下創建componentsB.vueVue.use(Router)export default new Router({	routes:[		{			path:'/componentsA',			name:'componentsA',			component:componentsA		},		{			path:'/componentsB',			name:'componentsB',			component:componentsB		}	]})

1.1 路由配置傳參

首先確定自己要傳的參數名,將路由配置修改一下,傳name,age,sex三個參數:

{			path:'/componentsB/:name/:age/:sex',			name:'componentsB',			component:componentsB		}

componentsA.vue 頁面通過 this.$router.push 配置與之對應的參數:

componentsA.vue

<template>	<div>		<div>我是組件A</div>		<button @click='routerToB1'>方式一跳轉到組件B</button>	</div></template><script>	export default{		data(){			return{				person:{name:'Gene',age:'18',sex:'male'}			}		},		methods: {			routerToB1() {				this.$router.push({					path:`componentsB/${this.person.name}/${this.person.age}/${this.person.sex}`				})			}		},	}</script><style></style>

然后在 componentsB.vue 頁面用 this.$route.params 接收參數:

componentsB.vue

<template>	<div>		<div>我是組件B</div>	</div></template><script>	export default{		created(){			this.getRouterData()		},		methods: {			getRouterData(){				const param = this.$route.params				console.log(param)//{name:'Gene',age:'18',sex:'male'}			}		},	}</script><style></style>

點擊按鈕"方式一跳轉到組件B",componentsB頁面打印出 {name:'Gene',age:'18',sex:'male'} ,成功獲取到A頁面傳過來的參數,并且地址欄顯示為 localhost:8889/#/componentsB/Gene/18/male (端口號根據自己設置的來),表明這種傳參方式url會攜帶參數。

1.2 params傳參

首先將剛才路由配置修改部分還原,在 componentsA.vue 頁面添加按鈕"方式二跳轉到組件B":

componentsA.vue

<template>	<div>		<div>我是組件A</div>		<button @click='routerToB1'>方式一跳轉到組件B</button>		<button @click='routerToB2'>方式二跳轉到組件B</button>	</div></template>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 来安县| 双辽市| 洞头县| 白城市| 奇台县| 龙口市| 军事| 鹤山市| 吉木萨尔县| 嘉峪关市| 桦南县| 松江区| 乌拉特中旗| 梨树县| 图木舒克市| 游戏| 清水县| 新和县| 通城县| 许昌市| 南和县| 丰顺县| 昌江| 东乌珠穆沁旗| 多伦县| 淮阳县| 鲁山县| 永新县| 孝感市| 成武县| 博兴县| 会同县| 焉耆| 手游| 车致| 汤原县| 台前县| 昭觉县| 华池县| 海林市| 郑州市|