How to handle $dispatch and $broadcast deprecations in Vue.js?

时间:2016-08-31 12:25:45

标签: vue.js vue-resource vue-component vue-router

I was working on a large-scale Vue.js application, and I have used broadcast and dispatch tons of places in my application. What's the best way to sit down and change all these? Or do I have some other way to deal with these breaking-changes?

2 个答案:

答案 0 :(得分:2)

Both these methods now are merged in the $emit method.

Unfortunately, replacing every instance of $broadcast and $dispatch with $emit will not work because the pattern used to manage events the one of the 'event emitter' now.

In my opinion the quickest way to deal with this deprecation is to create a spurious Vue instance and use it as an event hub.

答案 1 :(得分:1)

我最近遇到了与你相同的问题,因为我使用了很多$ broadcast和$ dispatch。解决方案是:

  • 如上一个答案所指出的,使用组件作为事件中心来管理您的应用程序状态;
  • 使用vuex。我花了一些时间来分析选项,并且真的觉得vuex是要走的路。 Vuex为您提供了一个管理所有应用程序状态和事件的中央结构,并提供了可以轻松带入组件的开箱即用的实用程序。它还使代码更加模块化,更容易重新排列和使用。最起码,我是这么想的。我在两周前开始使用它,我真的很激动。如果你花时间学习我觉得它可能值得。

干杯