Vue組件間的通信pubsub-js實(shí)現(xiàn)步驟解析
本文介紹使用發(fā)布訂閱的方式進(jìn)行vue組件間的通信
我認(rèn)為這種方式比較自由, 不存在組件間的關(guān)系問(wèn)題
1. 首先安裝pubsub-js
npm install --save pubsub-js
2. 訂閱方組件
import PubSub from ’pubsub-js’
mounted(){ // 執(zhí)行異常代碼 // 訂閱消息 PubSub.subscribe(’deleteTodo’,(msg,index)=>{ this.deleteTodo(index) // 調(diào)用deleteTodo方法執(zhí)行真正的業(yè)務(wù)邏輯 });},
3. 發(fā)布方組件
<script> import PubSub from ’pubsub-js’ export default{ methods: { handlerEnter(isEnter){ if (isEnter) { this.bgColor = ’gray’; this.isShow = true; } else { this.bgColor = ’white’; this.isShow = false; } }, deleteItem(){ // 表示從this對(duì)象中取出todo,index,deleteTodo三個(gè)對(duì)象 const {todo, index, deleteTodo} = this if (window.confirm(`確認(rèn)刪除${todo.title}嗎?`)) { // 發(fā)布消息 PubSub.publish(’deleteTodo’, index); //deleteTodo一定要與訂閱方名稱(chēng)一樣,index是通信的具體數(shù)據(jù) } } } }</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP實(shí)現(xiàn)加法驗(yàn)證碼2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)4. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. 小技巧處理div內(nèi)容溢出6. JSP實(shí)現(xiàn)文件上傳功能7. JavaWeb Servlet中url-pattern的使用8. asp知識(shí)整理筆記4(問(wèn)答模式)9. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能10. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲
