vue父組件監(jiān)聽子組件數(shù)據(jù)更新方式(hook)
vue父組件監(jiān)聽子組件數(shù)據(jù)更新
this.$on(‘hook : 生命周期鉤子’, () => {})
可以監(jiān)聽執(zhí)行生命周期鉤子,適用場合如:
data(){ ?? ?return { ?? ??? ?itemID:'', ?? ?} } mounted(){ ?? ?// 設(shè)置定時器 ?? ?this.itemID = setInterval(()=> { console.log('開計時器') }, 1000); ?? ?// 離開頁面時清除定時器 ?? ?this.$once('hook:beforeDestroy', ()=> { clearInterval(this.itemID) }) }
將定時器和清除定時器放在一起,功能和寫在beforeDestroy生命周期中是一樣滴,這樣維護起來比較方便 ~
定時器里放需要定時請求的數(shù)據(jù),然后離開頁面就清除定時器,也就停止請求
@hook:生命周期鉤子=“觸發(fā)的函數(shù)”
父組件監(jiān)聽子組件數(shù)據(jù)更新,也就是監(jiān)聽生命周期 ~
來人! 上代碼 !
// 父組件 <template> ?? ?<div class="father"> ?? ??? ?<son @hook:update="sonUpdateFun"/> ?// 我是子組件 ?? ?</div> </template>
<script> ?? ?import son from './son.vue' ?? ?export default { ?? ??? ?components:{ 'son' }, ?? ??? ?methods:{ ?? ??? ??? ?sonUpdateFun(){ ?? ??? ??? ??? ?console.log("子組件的數(shù)據(jù)更新啦~~~") ?? ??? ??? ?} ?? ??? ?} ?? ?} </script>
當(dāng)子組件的數(shù)據(jù)更新的時候就會觸發(fā)函數(shù) sonUpdateFun,就可以進行一些不可描述的操作了
監(jiān)聽子組件內(nèi)數(shù)據(jù)變化,父組件綁定change事件
如題,做了一個分頁功能,element里面的分頁,已經(jīng)有了頁碼改變、每頁數(shù)量改變的change事件,這樣如果當(dāng)頁碼改變、每頁數(shù)量改變的時候,又要在methods里面寫change來根據(jù)改變的值來獲取分頁,這樣就很麻煩,如果沒個頁面都有分頁,這樣就要在methods里面多寫change事件。
想到了一個解決方法,就是把element的分頁封裝成一個組件,具體思路如下:
子組件
1.這個綁定了每頁數(shù)量改變的事件,
handleSizeChange handleSizeChange(index) { this.query.length = index },
2.這個綁定了當(dāng)前頁碼改變的事件
handleCurrentChange handleCurrentChange(index) { console.log(index) this.query.start = index }
3.最重要的數(shù)據(jù),這里是當(dāng)前頁碼和每頁數(shù)量,
query: { start: 1, length: 10 }
4.頁碼跟數(shù)量改變是在子組件中完成的,寫個watch,然后實時把query傳遞給父組件
watch: { query: { handler(val, oldVal) { this.$emit('changeData', this.query) }, deep: true } },
5.完整子組件代碼
<template> <div> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="query.start" :page-sizes="[10, 20, 30, 50]" :page-size="query.length" layout="sizes, prev, pager, next" :total="total"> </el-pagination> </div> </template>
<script> export default { name: 'pagination', props: { size: { type: Number, default: function() { return 10 } }, total: { type: Number, default: function() { return 10 } }, start: { type: Number, default: function() { return 1 } } }, created() { this.query = { start: this.start, length: this.size } }, data() { return { query: { start: 1, length: 10 } } }, watch: { query: { handler(val, oldVal) { this.$emit('changeData', this.query) }, deep: true } }, methods: { handleSizeChange(index) { this.query.length = index }, handleCurrentChange(index) { console.log(index) this.query.start = index } } } </script> <style scoped> </style>
父組件
1.先引入組件,組件位置根據(jù)自己的而定
import pagination from '../../components/table-pagination/pagination'
2.然后注冊組件
components: { pagination },
3.使用組件
<pagination :size="query.length" :total="total" :start="query.start" @changeData="getList($event)" />
組件傳遞的參數(shù):
4.如何獲取子組件傳遞過來的query對象?
1)父組件獲取
注意:
這里的changeData跟子組件中$emit發(fā)送的地方要相同!
2)子組件部位
注意:this.$emit('changeData',this.query)
changeData就是父組件中子組件部位注冊的自定義事件,this.query就是要傳遞的值
3)我的獲取分頁的函數(shù)就是getList(query),直接在自定義事件中寫@changeData="getList($event)"
大功告成
這樣每當(dāng)子組件中query的值改變之后,父組件都會執(zhí)行分頁查詢請求,就可以實時更新了!
后記:自定義事件,可以做很多事情,比如,點擊子組件按鈕,傳遞參數(shù),然后父組件使用這個參數(shù)來做什么事情,當(dāng)整個頁面的數(shù)據(jù)量、邏輯實在是太龐大的時候,就可以把整個也面分成一個個小組件來,這樣就不會顯得頁面龐大復(fù)雜了…
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js中使用iView日期選擇器并設(shè)置開始時間結(jié)束時間校驗功能
本文通過實例代碼給大家介紹了Vue.js中使用iView日期選擇器并設(shè)置開始時間結(jié)束時間校驗功能,需要的朋友可以參考下2018-08-08vue項目中vue-i18n和element-ui國際化開發(fā)實現(xiàn)過程
這篇文章主要介紹了vue項目中vue-i18n和element-ui國際化開發(fā)實現(xiàn)過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04