首页
/
每日頭條
/
生活
/
vue如何接收傳參
vue如何接收傳參
更新时间:2024-10-08 02:17:37

vue如何接收傳參?1、路由通信傳值路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段,我來為大家科普一下關于vue如何接收傳參?以下内容希望對你有幫助!

vue如何接收傳參(vue中各種通信傳值方式總結)1

vue如何接收傳參

1、路由通信傳值

路由通信是通過路由跳轉用query把參數帶過去,也是vue常用的通信手段。

例子:創建并在路由注冊一個組件Head

<template> <div id="head"> <button @click="handleChange">clickMe</button> //給按鈕綁定點擊事件 </div> </template> <script> export default { name: 'Head', data () { return { } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path:"/about" , query:{ text:"我是阿格斯之盾" } }) //路由跳轉,并用query帶過去參數 } } } </script> <style scoped> </style>

創建另一個組件About并在路由注冊

<template> <div id="about"> <p>我是關于頁:{{ message }}</p><button type="button" @click="handleChange">回到首頁</button> //顯示接收過來的數據 </div> </template> <script> export default { name: 'About', data () { return { message: "" } }, mounted(){ this.message = this.$route.query.text //在生命周期中接收傳過來的數據 }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path: "/" }) //點擊返回首頁 } } } </script> <style scoped> </style>

路由注冊的簡單代碼

import Vue from 'vue' import Router from 'vue-router' import Head from '@/components/Head' import About from '@/components/About' Vue.use(Router) export default new Router({ mode: "history", routes: [ { path: '/', name: 'Head', component: Head },{ path: '/about', name: 'About', component: About } ] })

2、sessionStorage本地緩存通信

還是列舉上面的例子,将它們稍微改一改就可以了,路由配置都一樣的。sessionStorage的特點就是浏覽器關掉緩存就會消失,這是它區别于localStorage的。

例子: Heade代碼:

<template> <div id="head"> <button @click="handleChange">clickMe</button> </div> </template> <script> export default { name: 'Head', data () { return { } }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path:"/about"}) }, message(){ var message = "我是阿格斯之盾" sessionStorage.setItem('text', message) //創建緩存 } }, mounted(){ this.message(); } } </script> <style scoped> </style>

About代碼:

<template> <div id="about"> <p>我是關于頁:{{ message }}</p><button type="button" @click="handleChange">回到首頁</button> </div> </template> <script> export default { name: 'About', data () { return { message: "" } }, mounted(){ this.message = sessionStorage.getItem("text") //讀取緩存 }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path: "/" }) } } } </script> <style scoped> </style>

3、父組件向子組件通信

定義父組件Head,還是用上面的例子,父組件傳遞一句話給子組件,如果傳遞的參數很多,可使用json數組{}的形式。

例子: Head父組件代碼

<template> <div id="head"> <About :text=message></About> //将message參數傳給子組件 </div> </template> <script> import About from '@/components/About.vue' export default { name: 'Head', components:{ About }, data () { return { message : "我是阿格斯之盾" } }, mounted(){ }, methods:{ } } </script> <style scoped> </style>

About子組件代碼

<template> <div id="about"> <p>我是關于頁:{{ text }}</p> </div> </template> <script> export default { name: 'About', props:{ 'text':[] //子組件接受數據,[]裡面可以寫傳入類型,如果不符合會報錯 }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ } } } </script> <style scoped> </style>

4、子組件向父組件通信 子組件向父組件通信是通過emit事件發送的,話不多說,直接上案例,還是利用上面的案例稍作修改 About子組件代碼:

<template> <div id="about"> <button @click="handleChange">點擊發送消息給父組件</button> </div> </template> <script> export default { name: 'About', props:{ 'text':[] }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$emit( "child-message" , "我是阿格斯之盾" ) //提交信息 } } } </script> <style scoped> </style>

Head父組件代碼

<template> <div id="head"> <About @child-message = "handleText"></About> //這裡傳過來父組件需要用一個方法接住 <p>來自子組件的消息:{{message}}</p> </div> </template> <script> import About from '@/components/About.vue' export default { name: 'Head', components:{ About }, data () { return { message : "" } }, mounted(){ }, methods:{ handleText(data){ //這裡的data就是子組件傳過來的内容 this.message = data } } } </script> <style scoped> </style>

5、vuex狀态管理

【領取方式】關注頭條 前端全棧架構丶第一時間獲取最新前端資訊學習手機用戶可私信關鍵詞 【前端】即可獲取全棧工程師路線和學習資料!

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
早安心語鼓勵的話
早安心語鼓勵的話
早安心語鼓勵的話?真心想做事,再大的困難也可以克服;不想做事,再小的阻礙也會成為理由很多東西,先決條件很重要,但更重要的是後天自己付出的努力要記得,想成為成功的人,不要隻是說說而已從現在做起,從今天做起,人生的每一天每一刻,都是在為自己的明...
2024-10-08
閃退怎麼解決
閃退怎麼解決
閃退怎麼解決?緩存垃圾過多平時在使用軟件的過程中,會産生一些垃圾文件,如果長時間不清理會導緻手機越來越卡,也會出現閃退狀況,我來為大家科普一下關于閃退怎麼解決?以下内容希望對你有幫助!閃退怎麼解決緩存垃圾過多平時在使用軟件的過程中,會産生一...
2024-10-08
君度力嬌酒的家常做法
君度力嬌酒的家常做法
君度力嬌酒的家常做法?材料:Cointreau,冰塊首先拿出一隻酒杯,然後倒入适量的Cointreau,之後再放入适量的冰塊,即可,現在小編就來說說關于君度力嬌酒的家常做法?下面内容希望能幫助到你,我們來一起看看吧!君度力嬌酒的家常做法材料...
2024-10-08
怎樣才分得出腐竹的真假
怎樣才分得出腐竹的真假
作者:小伴公衆号:心照顧(公衆号ID:xinzhaogu)[關注居家養老、居家照顧,我們願意成為您依靠的港灣,做長期照顧者的好朋友、好夥伴,做爸爸媽媽的貼心小幫手]近日,在部分互聯網社交平台上流傳着兩段燃燒腐竹的視頻。視頻顯示,日常食用的腐...
2024-10-08
名聞天下是成語嗎
名聞天下是成語嗎
名聞天下是成語嗎?名聞天下,漢語成語,拼音是míngwéntiānxià,意思是形容名聲極大,今天小編就來聊一聊關于名聞天下是成語嗎?接下來我們就一起去研究一下吧!名聞天下是成語嗎名聞天下,漢語成語,拼音是míngwéntiānxià,意思...
2024-10-08
Copyright 2023-2024 - www.tftnews.com All Rights Reserved