Skip to content

Commit fc0afb5

Browse files
committed
add: automatic update mechanism
1 parent 6b8c154 commit fc0afb5

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"rollup-plugin-vue": "^5.1.6",
2323
"vue": "^2.6.11",
2424
"vue-template-compiler": "^2.6.11"
25+
},
26+
"dependencies": {
27+
"nanoid": "^2.1.11"
2528
}
2629
}

src/lazyload.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import nanoId from 'nanoid'
12
const DEFAULT_OBSERVER_OPTIONS = {
23
rootMargin: '0px',
34
threshold: 0
@@ -17,14 +18,18 @@ class Lazy {
1718
)
1819
}
1920
add(el, binding, vnode) {
21+
vnode.key = nanoId(4)
2022
this.ListenerQueue.push({
2123
loaded: false,
2224
el,
2325
binding,
2426
vnode
2527
})
2628
this.vue.nextTick(() => {
27-
this.observer && this.observer.observe(el)
29+
if (this.observer) {
30+
this.observer.unobserve(el)
31+
this.observer.observe(el)
32+
}
2833
})
2934
}
3035
observerHandler(entries, observer) {
@@ -46,6 +51,7 @@ class Lazy {
4651
}
4752
image.onerror = function() {}
4853
} else {
54+
// debugger
4955
const img = listener.el.querySelector('img')
5056
if (!img) {
5157
throw 'Please make sure that you have set img html inside v-lazy directive. [more details] https://github.com/YasinChan/vue-simple-lazyload/blob/master/README.md'

src/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const VueLazy = {
66
const lazy = new Lazy(Vue, options)
77

88
Vue.directive('lazy', {
9-
bind: lazy.add.bind(lazy)
9+
bind: lazy.add.bind(lazy),
10+
update: lazy.add.bind(lazy)
1011
})
1112
}
1213
}
@@ -16,7 +17,8 @@ export const VueImgLazy = {
1617
const lazy = new Lazy(Vue, options)
1718

1819
Vue.directive('lazy', {
19-
bind: lazy.add.bind(lazy)
20+
bind: lazy.add.bind(lazy),
21+
update: lazy.add.bind(lazy)
2022
})
2123
Vue.component(ImgLazy.name, ImgLazy)
2224
}

0 commit comments

Comments
 (0)