当前位置:网站首页>Vue communication and cross component listening state Vue communication
Vue communication and cross component listening state Vue communication
2020-11-06 20:48:00 【Papaya is too fragrant】
Hello everyone ! I am papaya, too fragrant !
as everyone knows , Component based development has brought us convenience , But it also introduces new problems , Data between components is like being separated by an invisible wall , If we want to temporarily have two components communicate directly ,vuex Too big , and $emit It's not easy to maintain provider Out of control This is the time for today's protagonist vue-communication Debut !
vue-communication Introduce
- It's an observable, debugged vue Component communication scheme
- Any relational component can communicate directly
- Support cross component monitoring of data changes
- Support sending offline data
install
yarn add vue-communication
// perhaps npm install vue-communication -D
introduce
import { $sender, $receiver } "vue-communication";
Vue.prototype.$sender = $sender;
Vue.prototype.$receiver = $receiver;
Video tutorial
usage
Basic concepts :
The component exposes two main API One is the sender $sender
One is the recipient $receiver
, All communication is achieved through these two methods , I hope you will $sender
The sender understood as the sending intention , He's sending an intention , Intention is what you want to do .
The type of intention : dataOnce
modifyOnce
data
modify
watch
At present, only the above 5 Each intention covers : Send data once and many times , Modify the data one and more times , Monitoring data changes across components
send data :
If A Components are directed only to B Component send once
data :
A Directly used in components this.$sender("dataOnce-A-B",{d: " I'm data "})
B Component at any time ( Even if the B It's ok if you haven't mounted it yet , Eat with ease ) Use this.$receiver("dataOnce-A-B")
This function returns one Promise object , direct .then Just receive
Send more than once this.$sender("data-A-B"," I'm data ")
, This method is called multiple times , The corresponding component will receive the data multiple times
Reception will change , It can't be used because it will be received many times Promise To achieve , Please append callback to parameter this.$receiver("data-A-B",function(data){ // data It's data })
Be careful :
Inside Intention modifier - Components 1- Components 2
Is mandatory , No matter how long your component name is , You have to give the complete ! The same is true for the modified data below , Corresponding to data-A-B This case , The whole string can be called a “ Intention ”,data It's called the intent modifier .
Receiving receipt :
I believe we all understand a truth , If you entrust A to B Delivery , stay A It should not be silent after delivery , It's about telling you , well ! I have already delivered what you asked me to send ! This is the receipt .
It should be noted that receipts are currently available only in one-time operations , for example dataOnce
modifyOnce
They passed $sender
Back to Promise The object gives , Let the sender know , When was the data I sent received
this.$sender("dataOnce-A-B"," Papaya is too fragrant ")
.then(flag => {
console.log(" The recipient has received the data !")
})
Modifying data :
A Used in components this.$sender("modifyOnce-A-B","name"," Papaya is too fragrant ")
Express A The component needs to be modified B In the component name Attributes change him to Papaya is too fragrant
B Used in components this.$receiver("modifyOnce-A-B","name")
You can complete the modification , Note that the second parameter must be passed , This is a license , It means that you approve of A Component modifies the current component's name attribute , If you make a mistake or don't transmit it, then the permission doesn't hold , It's an implementation that makes data changes predictable and forces developers to be more aware of what they're doing .
If you want to change obj Under the object name Then it can be written as this.$sender("modifyOnce-A-B","obj.name"," Papaya is too fragrant ")
If you want to modify it many times, you can refer to data An example of intention , Use modify The intention modifier is enough , Be careful , At present, there is no callback in the modification , If you want to know when the data was modified , You can listen inside the component itself .
Listen to data across components :
If A Component to listen to B In the component name Data changes :
A Components use this.$sender("watch-A-B","person.name",function(nv,ov){ // nv Represents the new value ov Old value })
B Components only need to be licensed once : this.$receiver("watch-A-B","person.name")
I built one myself web Front of the exchange skirt, interested can join in the exchange oh :245650187( free )237871108( charge ). Personal wechat : GD6570 Individual ball :718879459 Of course, you can also search Bili Bili papaya is too fragrant
版权声明
本文为[Papaya is too fragrant]所创,转载请带上原文链接,感谢
边栏推荐
- 美团内部讲座|周烜:华东师范大学的数据库系统研究
- Analysis of serilog source code -- how to use it
- It's time for your financial report to change to a more advanced style -- financial analysis cockpit
- 华为云微认证考试简介
- The importance of big data application is reflected in all aspects
- 大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
- To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
- nacos、ribbon和feign的簡明教程
- IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
- Pollard's Rho algorithm
猜你喜欢
意外的元素..所需元素..
How to turn data into assets? Attracting data scientists
jenkins安装部署过程简记
【:: 是什么语法?】
CCR coin frying robot: the boss of bitcoin digital currency, what you have to know
美团内部讲座|周烜:华东师范大学的数据库系统研究
A small goal in 2019 to become a blog expert of CSDN
Diamond standard
Small program introduction to proficient (2): understand the four important files of small program development
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
随机推荐
Share with Lianyun: is IPFs / filecoin worth investing in?
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
How to hide part of barcode text in barcode generation software
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
(1) ASP.NET Introduction to core3.1 Ocelot
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
ES6 learning notes (5): easy to understand ES6's built-in extension objects
jenkins安装部署过程简记
image operating system windows cannot be used on this platform
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
行为型模式之备忘录模式
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
Chainlink brings us election results into blockchain everipedia
解决 WPF 绑定集合后数据变动界面却不更新的问题
Building a new generation cloud native data lake with iceberg on kubernetes
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
Isn't data product just a report? absolutely wrong! There are university questions in this category
意外的元素..所需元素..
es创建新的索引库并拷贝旧的索引库 实践亲测有效!