vue組件中的數(shù)據(jù)傳遞方法
Vue 的組件作用域都是孤立的,不允許在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)。必須使用特定的方法才能實現(xiàn)組件之間的數(shù)據(jù)傳遞。組件之間傳遞數(shù)據(jù)大致分為三種情況:
父組件向子組件傳遞數(shù)據(jù),通過 props 傳遞數(shù)據(jù)。
子組件向父組件傳遞數(shù)據(jù),通過 events 傳遞數(shù)據(jù)。
兩個同級組件之間傳遞數(shù)據(jù),通過 event bus 傳遞數(shù)據(jù)。
一、父組件向子組件傳遞數(shù)據(jù)
子組件部分:
<template> <div class="child"> {{ msg }} </div> </template> <script> export default { name: 'child', data(){ return { } }, props: ['msg'] </script>
在child.vue中,msg實在data中定義的變量,使用props:['msg']從父組件中獲取msg的值
父組件部分:
<template> <div class="child"> child <child :msg="message"></child> </div> </template> <script> import child from './child.vue' export default { name: 'parent', components: { child }, data () { return { message: 'hello vue' } } } </script>
在調(diào)用組件的時候,使用v-bind將msg的值綁定為parent.vue中定義的變量message,這樣就能將parent.vue中的message的值傳給child.vue了。
單項數(shù)據(jù)流
當父組件的 message 發(fā)生改變,子組件也會自動地更新視圖。但是在子組件中,我們不要去修改 prop。如果你必須要修改到這些數(shù)據(jù),你可以使用以下方法:
方法一:把 prop 賦值給一個局部變量,然后需要修改的話就修改這個局部變量,而不影響 prop
export default { data(){ return { newMessage: null } }, props: ['message'], created(){ this.newMessage = this.message; } }
方法二:在計算屬性中對 prop 進行處理
export default { props: ['message'], computed: { newMessage(){ return this.message + ' 哈哈哈'; } } }
二、子組件向父組件傳遞數(shù)據(jù)
子組件主要通過實踐傳遞數(shù)據(jù)給父組件的
子組件部分:
<template> <div class="child"> <span>用戶名:</span> <input v-model="username" @change="sendUser" /> </div> </template>
子組件的html中,當input中的值發(fā)生改變時,將username傳遞給parent.vue。
首先聲明了一個sendUser方法,用change事件來調(diào)用sendUser。
<script> export default { name: 'parend', data () { return { username: '' } }, methods: { sendUser () { this.$emit('changeName', this.username) } } } </script>
在sendUser中,使用$emit來遍歷changeName事件,并返回this.name,其中changeName是一個自定義的事件,功能類似于一個中轉(zhuǎn),this.name將通過這個事件傳遞給父組件。
父組件部分:
<template> <div class="parent"> <child @changeName="getUser"></child> <p>用戶名:{{user}}</p> </div> </template>
在父組件中聲明了一個getUser方法,用changeName事件調(diào)用getUser方法,獲取從子組件傳遞過來的參數(shù)username
<script> import child from './child.vue' export default { name: 'parent', components: { child }, data () { return { user: '' } }, methods: { getUser(data) { this.user = data } } } </script>
getUser方法中的參數(shù)msg就是從子組件中傳遞過來的參數(shù)uesrname。
三、同級組件間的數(shù)據(jù)傳遞
有時候兩個組件也需要通信(非父子關(guān)系)。當然Vue2.0提供了Vuex,但在簡單的場景下,可以使用一個空的Vue實例作為中央事件總線。
<template> <div id="app"> <c1></c1> //組件1 <c2></c2> //組件2 </div> </template>
組件c1中:
<template> <div class="c1"> page <button @click="changeMsg">click</button> </div> </template> <script> var Bus = new Vue(); //為了方便將Bus(空vue)定義在一個組件中,在實際的運用中一般會新建一Bus.js export default { name: 'c1', data () { return { message: 'hi' } }, methods: { changeMsg () { //點擊按鈕,將this.message傳遞給c2 bus.$emit('sendMsg', this.message) } } } </script>
組件c2中:
<template> <div class="c2"> {{msg}} </div> </template> <script> var Bus = new Vue(); export default { name: 'c2', data () { return { msg: '' } }, created () { bus.$on('sendMsg',(data)=>{ //data即為c1組件中的message this.msg = data }) } } </script>
在實際運用中,一般將bus抽離出來:
//Bus.js import Vue from 'vue' const Bus = new Vue() expore default Bus
組件調(diào)用時引用(import Bus from './Bus.js')
但這種引入方式,經(jīng)過webpack打包后可能會出現(xiàn)Bus局部作用域的情況,即引用的是兩個不同的Bus,導致不能正常通信
實際運用:
將Bus注入到Vue根對象中
import Vue from 'vue' const Bus = new Vue() var app= new Vue({ el:'#app', data:{ Bus } })
在子組件中通過this.$root.Bus.$on(),this.$root.Bus.$emit()
來調(diào)用
總結(jié)
以上所述是小編給大家介紹的vue組件中的數(shù)據(jù)傳遞方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Vuejs第九篇之組件作用域及props數(shù)據(jù)傳遞實例詳解
- vue父子組件的數(shù)據(jù)傳遞示例
- Vue.js實戰(zhàn)之組件之間的數(shù)據(jù)傳遞
- Vue 父子組件的數(shù)據(jù)傳遞、修改和更新方法
- Vue2.0組件間數(shù)據(jù)傳遞示例
- vuejs2.0實現(xiàn)分頁組件使用$emit進行事件監(jiān)聽數(shù)據(jù)傳遞的方法
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- Vue 父子組件數(shù)據(jù)傳遞的四種方式( inheritAttrs + $attrs + $listeners)
- Vuejs 組件——props數(shù)據(jù)傳遞的實例代碼
- vue組件之間數(shù)據(jù)傳遞的方法實例分析
相關(guān)文章
Vue時間軸 vue-light-timeline的用法說明
這篇文章主要介紹了Vue時間軸 vue-light-timeline的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10WebStorm啟動vue項目報錯代碼:1080?throw?err解決辦法
在使用webstorm新建vue項目時常會遇到一些報錯,下面這篇文章主要給大家介紹了關(guān)于WebStorm啟動vue項目報錯代碼:1080?throw?err的解決辦法,文中將解決辦法介紹的非常詳細,需要的朋友可以參考下2023-12-12vue.js中created()與activated()的個人使用解讀
這篇文章主要介紹了vue.js中created()與activated()的個人使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07nuxt框架中對vuex進行模塊化設置的實現(xiàn)方法
這篇文章主要介紹了nuxt框架中對vuex進行模塊化設置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09vue+element下日期組件momentjs轉(zhuǎn)換賦值問題解決
這篇文章主要介紹了vue+element下日期組件momentjs轉(zhuǎn)換賦值問題,記錄下使用momentjs轉(zhuǎn)換日期字符串賦值給element的日期組件報錯問題,需要的朋友可以參考下2024-02-02