fix(Lazyload): directive update is removed

This commit is contained in:
chenjiahan 2020-11-30 19:31:37 +08:00
parent b6a9245740
commit 5d8b7bd7fb
2 changed files with 10 additions and 3 deletions

View File

@ -26,8 +26,7 @@ export const Lazyload = {
app.directive('lazy', { app.directive('lazy', {
beforeMount: lazy.add.bind(lazy), beforeMount: lazy.add.bind(lazy),
update: lazy.update.bind(lazy), updated: lazy.update.bind(lazy),
updated: lazy.lazyLoadHandler.bind(lazy),
unmounted: lazy.remove.bind(lazy), unmounted: lazy.remove.bind(lazy),
}); });

View File

@ -9,14 +9,16 @@ export default (lazy) => {
default: 'div', default: 'div',
}, },
}, },
emits: ['show'], emits: ['show'],
render() { render() {
return h( return h(
this.tag, this.tag,
null,
this.show && this.$slots.default ? this.$slots.default() : null this.show && this.$slots.default ? this.$slots.default() : null
); );
}, },
data() { data() {
return { return {
el: null, el: null,
@ -27,18 +29,22 @@ export default (lazy) => {
show: false, show: false,
}; };
}, },
mounted() { mounted() {
this.el = this.$el; this.el = this.$el;
lazy.addLazyBox(this); lazy.addLazyBox(this);
lazy.lazyLoadHandler(); lazy.lazyLoadHandler();
}, },
beforeUnmount() { beforeUnmount() {
lazy.removeComponent(this); lazy.removeComponent(this);
}, },
methods: { methods: {
getRect() { getRect() {
this.rect = this.$el.getBoundingClientRect(); this.rect = this.$el.getBoundingClientRect();
}, },
checkInView() { checkInView() {
this.getRect(); this.getRect();
return ( return (
@ -49,11 +55,13 @@ export default (lazy) => {
this.rect.right > 0 this.rect.right > 0
); );
}, },
load() { load() {
this.show = true; this.show = true;
this.state.loaded = true; this.state.loaded = true;
this.$emit('show', this); this.$emit('show', this);
}, },
destroy() { destroy() {
return this.$destroy; return this.$destroy;
}, },