vue中各種通信傳值方式總結(jié)
1、路由通信傳值
路由通信是通過(guò)路由跳轉(zhuǎn)用query把參數(shù)帶過(guò)去,也是vue常用的通信手段。
例子:創(chuàng)建并在路由注冊(cè)一個(gè)組件Head
<template> <div id="head"> <button @click="handleChange">clickMe</button> //給按鈕綁定點(diǎn)擊事件 </div> </template> <script> export default { name: 'Head', data () { return { } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path:"/about" , query:{ text:"我是阿格斯之盾" } }) //路由跳轉(zhuǎn),并用query帶過(guò)去參數(shù) } } } </script> <style scoped> </style>
創(chuàng)建另一個(gè)組件About并在路由注冊(cè)
<template> <div id="about"> <p>我是關(guān)于頁(yè):{{ message }}</p><button type="button" @click="handleChange">回到首頁(yè)</button> //顯示接收過(guò)來(lái)的數(shù)據(jù) </div> </template> <script> export default { name: 'About', data () { return { message: "" } }, mounted(){ this.message = this.$route.query.text //在生命周期中接收傳過(guò)來(lái)的數(shù)據(jù) }, updated(){ }, methods:{ handleChange(){ this.$router.push({ path: "/" }) //點(diǎn)擊返回首頁(yè) } } } </script> <style scoped> </style>
路由注冊(cè)的簡(jiǎn)單代碼
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的特點(diǎn)就是瀏覽器關(guān)掉緩存就會(huì)消失,這是它區(qū)別于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) //創(chuàng)建緩存 } }, mounted(){ this.message(); } } </script> <style scoped> </style>
About代碼:
<template> <div id="about"> <p>我是關(guān)于頁(yè):{{ message }}</p><button type="button" @click="handleChange">回到首頁(yè)</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,還是用上面的例子,父組件傳遞一句話給子組件,如果傳遞的參數(shù)很多,可使用json數(shù)組{}的形式。
例子: Head父組件代碼
<template> <div id="head"> <About :text=message></About> //將message參數(shù)傳給子組件 </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>我是關(guān)于頁(yè):{{ text }}</p> </div> </template> <script> export default { name: 'About', props:{ 'text':[] //子組件接受數(shù)據(jù),[]里面可以寫傳入類型,如果不符合會(huì)報(bào)錯(cuò) }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ } } } </script> <style scoped> </style>
4、子組件向父組件通信 子組件向父組件通信是通過(guò)emit事件發(fā)送的,話不多說(shuō),直接上案例,還是利用上面的案例稍作修改 About子組件代碼:
<template> <div id="about"> <button @click="handleChange">點(diǎn)擊發(fā)送消息給父組件</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> //這里傳過(guò)來(lái)父組件需要用一個(gè)方法接住 <p>來(lái)自子組件的消息:{{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就是子組件傳過(guò)來(lái)的內(nèi)容 this.message = data } } } </script> <style scoped> </style>
5、vuex狀態(tài)管理
狀態(tài)管理使用起來(lái)相對(duì)復(fù)雜,但是對(duì)于大型項(xiàng)目確實(shí)非常實(shí)用的。
(1)安裝vuex,并建立倉(cāng)庫(kù)文件
npm install vuex -s
安裝過(guò)后在src文件中創(chuàng)建store文件夾,并建立index.js文件,index.js的代碼如下:
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { message: '我是阿格斯之盾' }, mutations: { MESSAGE_INFO (state,view) { state.message = view; } } }) export default store
(2)在min.js中注冊(cè)store倉(cāng)庫(kù) 代碼如下:
import Vue from 'vue' import App from './App' import router from './router' import store from './store' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
(3)狀態(tài)的讀取和提交 還是使用上面的案例,我們以子組件About提交改變狀態(tài),父組件Head接受狀態(tài)并顯示出來(lái)下面是About組件提交狀態(tài)
<template> <div id="about"> <button @click="handleChange">點(diǎn)擊發(fā)送消息給父組件</button> </div> </template> <script> export default { name: 'About', props:{ 'text':[] }, data () { return { message: "" } }, mounted(){ }, updated(){ }, methods:{ handleChange(){ this.$store.commit("MESSAGE_INFO" , "我是火車王") //提交改變狀態(tài) } } } </script> <style scoped> </style>
Head組件接受狀態(tài):
<template> <div id="head"> <About></About> <p>來(lái)自子組件的消息:{{this.$store.state.message}}</p> //直接使用this.$store.state.message接受數(shù)據(jù)顯示 </div> </template> <script> import About from '@/components/About.vue' export default { name: 'Head', components:{ About }, data () { return { message : "" } }, mounted(){ }, methods:{ } } </script> <style scoped> </style>
總結(jié):以上就是vue中的通信方式,當(dāng)然還有一些,比如說(shuō)eventBus什么的,適用于中小型項(xiàng)目,但是我用的比較少,一般上面的幾種在開(kāi)發(fā)中已經(jīng)夠用的,例子很簡(jiǎn)單,學(xué)習(xí)是永無(wú)止境的,具體更深的東西還得下功夫去研讀官網(wǎng)或者其他資料,本文中有不對(duì)的地方或者疑惑的地方,還望大家多多指教!謝謝。
相關(guān)文章
查找Vue中下標(biāo)的操作(some和findindex)
這篇文章主要介紹了查找Vue中下標(biāo)的操作(some和findindex),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue項(xiàng)目 npm run build 打包項(xiàng)目防止瀏覽器緩存的操作方法
這篇文章主要介紹了vue項(xiàng)目 npm run build 打包項(xiàng)目防止瀏覽器緩存的操作方法,本文給大家推薦兩種方法,每種方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08利用Vue-cli搭建Vue項(xiàng)目框架的教程詳解
這篇文章主要為大家詳細(xì)介紹了利用Vue-cli搭建Vue項(xiàng)目框架的相關(guān)資料,對(duì)大家深入了解Vue有一定的幫助,感興趣的小伙伴可以了解一下2023-02-02uni-popup手寫菜鳥(niǎo)上門取件時(shí)間選擇器
這篇文章主要為大家介紹了uni-popup手?jǐn)]了一個(gè)菜鳥(niǎo)上門取件時(shí)間選擇器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08快速解決Vue項(xiàng)目在IE瀏覽器中顯示空白的問(wèn)題
今天小編就為大家分享一篇快速解決Vue項(xiàng)目在IE瀏覽器中顯示空白的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue-cli 3.0 自定義vue.config.js文件,多頁(yè)構(gòu)建的方法
今天小編就為大家分享一篇vue-cli 3.0 自定義vue.config.js文件,多頁(yè)構(gòu)建的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09