当前位置:首页 > 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)
    }
  }
}

在模板中使用:

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

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

vue watch实现懒加载

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

相关文章

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…

vue2实现图片懒加载

vue2实现图片懒加载

Vue2 图片懒加载实现方法 使用 Intersection Observer API Intersection Observer API 是浏览器原生支持的懒加载方案,性能较好且无需额外库。 /…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用…