1.0.2 • Published 5 years ago

vue-connection-listener v1.0.2

Weekly downloads
19
License
MIT
Repository
github
Last release
5 years ago

vue-connection-listener

Vue event bus plugin listening for online/offline changes

screenshot

About

Whenever the navigator's connection status changes a 'connection' event is emitted with a boolean payload indicating the new state (online = true, offline = false).

Installation

npm install --save vue-connection-listener

npm package link

Usage

main.js

import Vue from "Vue";
import bus from "./bus"; // Event bus instance

Vue.prototype.$bus = bus; // Optional (but convenient)

import VueConnectionListener from "vue-connection-listener";
const connectionListener = new VueConnectionListener(bus); // Create instance (injecting our bus)

new Vue({
  el: "#app",
  render: h => h(App),
  created() {
    connectionListener.register();
  },
  destroyed() {
    connectionListener.unregister();
  }
});

Inside any component

this.$bus.$on("connection", online => {
  alert(online ? "You are online 😄" : "You are offline 😞");
});

Notes

You need to import your own event bus and then inject it as a dependency.

Creating a bus could be as simple as creating a bus.js file with the following contents:

import Vue from "vue";
export default new Vue();

For more info:

https://alligator.io/vuejs/global-event-bus/

1.0.2

5 years ago

1.0.1

5 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago