TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Building a Simple EventBus in Vue.js

2 pointsby arvidkahlover 8 years ago

1 comment

StreakyCobraover 8 years ago
&gt; In a larger Vue.js project, you would likely abstract the Event Bus into a single file, importing it into every component file where it needs to be used. This way, it does not pollute the global namespace.<p>Instead of having to import a file everywhere, I&#x27;m used to add the bus to the Vue prototype:<p><pre><code> &#x2F;&#x2F; Create a bus for global events var busVue = new Vue() &#x2F;&#x2F; vue properties Object.defineProperties(Vue.prototype, { $bus: { get: function () { return busVue } } } </code></pre> so I can simply use it from any Vue component:<p><pre><code> this.$bus.$emit(&#x27;error&#x27;, &#x27;Impossible to connect to the database&#x27;) </code></pre> for emitting, and for binding on:<p><pre><code> mounted: function (msg) { this.$bus.$on(&#x27;error&#x27;, this.alert) }</code></pre>
评论 #13623570 未加载