当前位置:首页 > VUE

vue watch实现懒加载

2026-02-21 22:23:58VUE

使用 Vue Watch 实现懒加载

在 Vue 中,watch 可以监听数据变化,结合懒加载技术,可以实现按需加载数据或组件。以下是几种常见实现方式:

监听滚动事件实现懒加载

通过监听滚动事件,结合 watch 判断元素是否进入可视区域:

data() {
  return {
    isLoading: false,
    items: [],
    currentPage: 1
  }
},
watch: {
  isLoading(newVal) {
    if (newVal) {
      this.loadMoreData()
    }
  }
},
mounted() {
  window.addEventListener('scroll', this.handleScroll)
},
methods: {
  handleScroll() {
    const scrollPosition = window.innerHeight + window.scrollY
    const pageHeight = document.documentElement.scrollHeight
    if (scrollPosition >= pageHeight - 200 && !this.isLoading) {
      this.isLoading = true
    }
  },
  loadMoreData() {
    fetch(`/api/items?page=${this.currentPage}`)
      .then(res => res.json())
      .then(data => {
        this.items = [...this.items, ...data]
        this.currentPage++
        this.isLoading = false
      })
  }
}

使用 Intersection Observer API

更现代的方式是使用 Intersection Observer API,性能更好:

data() {
  return {
    observer: null,
    items: [],
    currentPage: 1
  }
},
mounted() {
  this.observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        this.loadMoreData()
      }
    })
  }, { threshold: 0.1 })

  this.observer.observe(document.querySelector('#load-more-trigger'))
},
methods: {
  loadMoreData() {
    fetch(`/api/items?page=${this.currentPage}`)
      .then(res => res.json())
      .then(data => {
        this.items = [...this.items, ...data]
        this.currentPage++
      })
  }
}

组件懒加载

对于 Vue 组件,可以使用动态导入实现懒加载:

components: {
  'lazy-component': () => import('./LazyComponent.vue')
}

结合 watch 实现按需加载:

data() {
  return {
    showLazyComponent: false
  }
},
watch: {
  showLazyComponent(newVal) {
    if (newVal) {
      this.$nextTick(() => {
        import('./LazyComponent.vue').then(component => {
          this.$options.components['lazy-component'] = component
        })
      })
    }
  }
}

图片懒加载

实现图片懒加载的示例:

directives: {
  lazy: {
    inserted(el, binding) {
      const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
          if (entry.isIntersecting) {
            el.src = binding.value
            observer.unobserve(el)
          }
        })
      })
      observer.observe(el)
    }
  }
}

在模板中使用:

vue watch实现懒加载

<img v-lazy="imageUrl" alt="Lazy loaded image">

这些方法展示了如何利用 Vue 的 watch 功能结合不同技术实现懒加载效果,可以根据具体需求选择合适的方式。

标签: 加载vue
分享给朋友:

相关文章

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…