vue-network
Version:
Render a Vue component to indicate the network status.
56 lines (43 loc) • 1.3 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.VueNetwork = factory());
}(this, (function () { 'use strict';
var Network = {
name: 'network',
props: {
changedOnly: Boolean
},
data: function data() {
return {
isOnline: typeof window === 'undefined' ? true : navigator.onLine
}
},
mounted: function mounted() {
window.addEventListener('online', this.whenOnline);
window.addEventListener('offline', this.whenOffline);
},
beforeDestroy: function beforeDestroy() {
window.removeEventListener('online', this.whenOnline);
window.removeEventListener('offline', this.whenOffline);
},
methods: {
whenOnline: function whenOnline() {
this.$emit('changed', true);
this.isOnline = true;
},
whenOffline: function whenOffline() {
this.$emit('changed', false);
this.isOnline = false;
}
},
render: function render() {
if (this.changedOnly && !this._isMounted) { return }
var ref = this.$slots;
var online = ref.online;
var offline = ref.offline;
return this.isOnline ? online && online[0] : offline && offline[0]
}
};
return Network;
})));