今天小編給大家分享一下vuejs中如何實(shí)現(xiàn)父子組件間數(shù)據(jù)交互的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
興安網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),興安網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為興安上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的興安做網(wǎng)站的公司定做!
父子組件之間的數(shù)據(jù)交互遵循:
props down - 子組件通過props接受父組件的數(shù)據(jù)
events up - 父組件監(jiān)聽子組件$emit的事件來操作數(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)聽事件
復(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è)對象
其實(shí)還有更多的場景需要組件間通信
官方推薦的通信方式
首選使用Vuex
使用事件總線:eventBus,允許組件自由交流
具體可見:$dispatch 和 $broadcast替換
以上就是“vuejs中如何實(shí)現(xiàn)父子組件間數(shù)據(jù)交互”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:vuejs中如何實(shí)現(xiàn)父子組件間數(shù)據(jù)交互
路徑分享:http://chinadenli.net/article42/jgjoec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計(jì)公司、面包屑導(dǎo)航、企業(yè)建站、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)