vuejs父子組件之間數(shù)據(jù)交互詳解
父子組件之間的數(shù)據(jù)交互遵循:
props down - 子組件通過props接受父組件的數(shù)據(jù)
events up - 父組件監(jiān)聽子組件$emit的事件來操作數(shù)據(jù)
示例
子組件的點擊事件函數(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)聽事件
父組件的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ù)的傳遞是一個對象
其實還有更多的場景需要組件間通信
官方推薦的通信方式
- 首選使用Vuex
- 使用事件總線:eventBus,允許組件自由交流
- 具體可見:$dispatch 和 $broadcast替換
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue父子組件的數(shù)據(jù)傳遞示例
- Vue 父子組件的數(shù)據(jù)傳遞、修改和更新方法
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- Vue 父子組件數(shù)據(jù)傳遞的四種方式( inheritAttrs + $attrs + $listeners)
- vue2.0父子組件間傳遞數(shù)據(jù)的方法
- vue組件之間數(shù)據(jù)傳遞的方法實例分析
- Vue組件之單向數(shù)據(jù)流的解決方法
- vue兄弟組件傳遞數(shù)據(jù)的實例
- Vue.js路由組件vue-router使用方法詳解
- vue-router:嵌套路由的使用方法
- vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能示例
相關(guān)文章
vue+openlayers+nodejs+postgis實現(xiàn)軌跡運動效果
使用postgres(postgis)數(shù)據(jù)庫以及nodejs作為后臺,vue和openlayers做前端,openlayers使用http請求通過nodejs從postgres數(shù)據(jù)庫獲取數(shù)據(jù),這篇文章主要介紹了vue+openlayers+nodejs+postgis實現(xiàn)軌跡運動,需要的朋友可以參考下2024-05-05
electron-vite工具打包后如何通過內(nèi)置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動態(tài)修改接口地址實現(xiàn)方法,需要的朋友可以參考下2024-05-05
vue parseHTML函數(shù)源碼解析 AST預(yù)備知識
這篇文章主要為大家介紹了vue parseHTML函數(shù)源碼解析 AST預(yù)備知識示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

