Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use eventbus to transfer values between components in vue

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "how to use eventbus to transfer values between components in vue" is not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use eventbus to transfer values between components in vue" article.

The way to use eventbus is simple. We need to do three things. First, we need to create a container to act as our eventbus.

Second, we need to throw, or submit, our event.

The third step, we go to monitor our incident (maybe this is the second part)

First, we need to define our eventbus globally

Here we define eventbus. This simply completes our first step, of course, the global variable, and I think you should know where the definition is.

Then let's throw this event first and use. Emit to "submit"

In any case, this is understandable. Secondly, we take the third step to monitor.

That's for sure. It's been monitored here. Okay. The click event is just an encumbrance.

And then we're going to use them in the interface.

First, pour in the documents we need:

What I use here is to talk about transimissionone and transimissiontwo.

Then there is the definition.

The second is to use the

Finally, run our project to check the results.

This is mainly for everyone to use, so the code is captured below, mainly four files.

Transimissionone . Vue (the file that sent the event)

Click to send values to eventbus export default {name: "transimission1", methods: {get: function () {console.log ("Aaa"); eventBus.$emit ('eventBusName', "hellokugou");}},}

Followed by transimissiontwo (listener)

Click console.log to output the eventbus information export default {name: "transimissiontwo", methods: {method1: function () {/ / use the on old listening event eventBus.$on ('eventBusName', function (val) {console.log ("this is with the value of transimissiontwo:" + val)}.

And then there's our hub. App . Used in vue

Test whether you can listen to the foo page, header page, hello page, import value from'. / components/value' import click from ". / components/click" import my_plug_ In from ". / components/plug_in" import sendparent from ". / components/send_parent" import testmintui from ". / components/Test_mint-ui" import transimissiontwo from ". / components/transimissiontwo" import transimissionone from ". / components/transimissionone" export default {name: 'app' Data () {return {netlogo: "Home page displays information to components"}}, components: {value, click, my_plug_in, sendparent, testmintui, transimissionone, transimissiontwo,}, methods: {getmessagefromchild: function (data) {console.log (data) } body {background-color: # f8f8ff; font-family: 'Avenir', Helvetica, Arial, sans-serif; color: # 2c3e50;} ul {width: 12remt;} ul li {list-style: none;} ul li:not (: last-child) {list-style: none; width: 2Reme; margin-left: 0.1rem Margin-right: 0.1rem. float: left; text-align: center; background: # 2C3E50; color: white;} ul li a {text-decoration: none; font-size: 16px; color: white; line-height: 1remt; text-align: center;} ul li:nth-child {list-style: none; clear: both;} .choose _ div {width: 100% Overflow: scroll;}

Please ignore the useless code. Then it's time to define eventbus.

Window.eventBus = new Vue (); the above is the content of this article on "how to use eventbus to transfer values between components in vue". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report