Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }