今天就跟大家聊聊有關(guān)vuejs中怎么實(shí)現(xiàn)父子組件間數(shù)據(jù)交互,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
父子組件之間的數(shù)據(jù)交互遵循:
props down - 子組件通過(guò)props接受父組件的數(shù)據(jù)
events up - 父組件監(jiān)聽(tīng)子組件$emit的事件來(lái)操作數(shù)據(jù)
示例
子組件的點(diǎn)擊事件函數(shù)中$emit自定義事件
export default { name: 'comment', props: ['issue','index'], data () { return { comment: '', } }, components: {}, methods: { removeComment: function(index,cindex) { this.$emit('removeComment', {index:index, cindex:cindex}); }, saveComment: function(index) { this.$emit('saveComment', {index: index, comment: this.comment}); this.comment=""; } }, //hook created: function () { //get init data } }
父組件監(jiān)聽(tīng)事件
復(fù)制代碼 代碼如下:
<comment v-show="issue.show_comments" :issue="issue" :index="index" @removeComment="removeComment" @saveComment="saveComment"></comment>
父組件的methods中定義了事件處理程序
removeComment: function(data) { var index = data.index, cindex = data.cindex; var issue = this.issue_list[index]; var comment = issue.comments[cindex]; axios.get('comment/delete/cid/'+comment.cid) .then(function (resp) { issue.comments.splice(cindex,1); }); }, saveComment: function(data) { var index = data.index; var comment = data.comment; var that = this; var issue =that.issue_list[index]; var data = { iid: issue.issue_id, content: comment }; axios.post('comment/save/',data) .then(function (resp) { issue.comments=issue.comments||[]; issue.comments.push({ cid: resp.data, content: comment }); }); //clear comment input this.comment=""; } },
注意參數(shù)的傳遞是一個(gè)對(duì)象
其實(shí)還有更多的場(chǎng)景需要組件間通信
官方推薦的通信方式
選使用Vuex
使用事件總線(xiàn):eventBus,允許組件自由交流
具體可見(jiàn):$dispatch 和 $broadcast替換
看完上述內(nèi)容,你們對(duì)vuejs中怎么實(shí)現(xiàn)父子組件間數(shù)據(jù)交互有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝大家的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱(chēng):vuejs中怎么實(shí)現(xiàn)父子組件間數(shù)據(jù)交互-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://aaarwkj.com/article8/ccpcop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、商城網(wǎng)站、電子商務(wù)、響應(yīng)式網(wǎng)站、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容