@Eazymov/vue-sub
Simple observer pattern implementation for VueJS
Dependencies
Installation
<script />
include:
Direct Include VueSub after vue. Plugin will be installed automatically.
NPM
npm install vue-sub --save
Yarn
yarn add vue-sub
When used with a module system, you must explicitly install VueSub via Vue.use():
;; Vue;
You don't need to do this when using global script tags.
Usage
Creating the observable
;; Vue; const observable = ; const app = observable;
You can create VueSub instance with existing observers
const observable = observers: 'log': consolelog anotherLogHandler 'error': consoleerror anotherErrorHandler ;
Using inside the component
const componentA = { this$observable; } const componentB = { this$observable; }
To unsubscribe from an event use $observable.unsubscribe
const component = { this$observable; this$observable; this$observable; // won't work }
If you want to react to an event only once, you can use $observable.once
const component = { this$observable; this$observable; // fires only once }
When using class components and typescript
;; @Component @ public void; @ public : void // Fires every time 'bar' emits @ public : void // Fires only once public : void this; // -> $observable.fire('foo', params);
Questions
If you have any troubles or questions you can create the issue or text me on email any time
License
Copyright (c) 2017 - present, Eduard Troshin