当前位置:首页 > 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 实现思路 核心是数据劫持结合发布-订阅模式,通过 Object.defineProperty 或 Proxy 监听数据变化,触发视图更新。 数据劫持实现 class Observer…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…