diff --git a/src/index.ts b/src/index.ts index 0e510da..7fe12cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,41 +5,42 @@ import LazyImage from './lazy-image' import { VueLazyloadOptions } from '../types/lazyload' import { App } from 'vue' -export default { - /* - * install function - * @param {Vue} Vue - * @param {object} options lazyload options - */ - install (Vue: App, options: VueLazyloadOptions = {}) { - const lazy = new Lazy(options) - const lazyContainer = new LazyContainer(lazy) - - const vueVersion = Number(Vue.version.split('.')[0]) - if (vueVersion < 3) return new Error('Vue version at least 3.0') - - Vue.config.globalProperties.$Lazyload = lazy - - Vue.provide('Lazyload', lazy) - - if (options.lazyComponent) { - Vue.component('lazy-component', LazyComponent(lazy)) - } - - if (options.lazyImage) { - Vue.component('lazy-image', LazyImage(lazy)) - } - - Vue.directive('lazy', { - beforeMount: lazy.add.bind(lazy), - beforeUpdate: lazy.update.bind(lazy), - updated: lazy.lazyLoadHandler.bind(lazy), - unmounted: lazy.remove.bind(lazy) - }) - Vue.directive('lazy-container', { - beforeMount: lazyContainer.bind.bind(lazyContainer), - updated: lazyContainer.update.bind(lazyContainer), - unmounted: lazyContainer.unbind.bind(lazyContainer) - }) + +/* +* install function +* @param {Vue} Vue +* @param {object} options lazyload options +*/ +const install = (Vue: App, options: VueLazyloadOptions = {}) => { + const lazy = new Lazy(options) + const lazyContainer = new LazyContainer(lazy) + + const vueVersion = Number(Vue.version.split('.')[0]) + if (vueVersion < 3) return new Error('Vue version at least 3.0') + + Vue.config.globalProperties.$Lazyload = lazy + + Vue.provide('Lazyload', lazy) + + if (options.lazyComponent) { + Vue.component('lazy-component', LazyComponent(lazy)) } + + if (options.lazyImage) { + Vue.component('lazy-image', LazyImage(lazy)) + } + + Vue.directive('lazy', { + beforeMount: lazy.add.bind(lazy), + beforeUpdate: lazy.update.bind(lazy), + updated: lazy.lazyLoadHandler.bind(lazy), + unmounted: lazy.remove.bind(lazy) + }) + Vue.directive('lazy-container', { + beforeMount: lazyContainer.bind.bind(lazyContainer), + updated: lazyContainer.update.bind(lazyContainer), + unmounted: lazyContainer.unbind.bind(lazyContainer) + }) } + +export { install }