当前位置:首页 > VUE

vue实现图片组件

2026-03-10 08:06:12VUE

Vue 图片组件的实现方法

基础图片组件实现

创建一个基础的图片组件,支持动态传入图片路径和替代文本。

<template>
  <img :src="src" :alt="alt" :class="className" />
</template>

<script>
export default {
  name: 'BaseImage',
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    className: {
      type: String,
      default: ''
    }
  }
}
</script>

懒加载图片组件

使用Intersection Observer API实现图片懒加载,提升页面性能。

<template>
  <img
    ref="image"
    :src="placeholder"
    :alt="alt"
    :class="className"
    @load="handleLoad"
  />
</template>

<script>
export default {
  name: 'LazyImage',
  props: {
    src: {
      type: String,
      required: true
    },
    placeholder: {
      type: String,
      default: 'data:image/png;base64,...'
    },
    alt: {
      type: String,
      default: ''
    },
    className: {
      type: String,
      default: ''
    }
  },
  mounted() {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          this.$refs.image.src = this.src
          observer.unobserve(this.$refs.image)
        }
      })
    })
    observer.observe(this.$refs.image)
  },
  methods: {
    handleLoad() {
      this.$emit('loaded')
    }
  }
}
</script>

响应式图片组件

根据设备屏幕大小加载不同尺寸的图片。

<template>
  <picture>
    <source 
      v-for="(source, index) in sources" 
      :key="index"
      :media="source.media"
      :srcset="source.srcset"
    >
    <img :src="fallback" :alt="alt" :class="className">
  </picture>
</template>

<script>
export default {
  name: 'ResponsiveImage',
  props: {
    sources: {
      type: Array,
      required: true,
      validator: value => value.every(item => 
        'media' in item && 'srcset' in item
      )
    },
    fallback: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    },
    className: {
      type: String,
      default: ''
    }
  }
}
</script>

图片加载状态处理

添加加载中和加载失败的状态处理。

vue实现图片组件

<template>
  <div class="image-container">
    <img
      v-show="!isLoading && !hasError"
      :src="src"
      :alt="alt"
      @load="handleLoad"
      @error="handleError"
    >
    <div v-if="isLoading" class="loading-placeholder">
      <!-- 加载中状态 -->
    </div>
    <div v-if="hasError" class="error-placeholder">
      <!-- 加载失败状态 -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageWithStatus',
  props: {
    src: {
      type: String,
      required: true
    },
    alt: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isLoading: true,
      hasError: false
    }
  },
  methods: {
    handleLoad() {
      this.isLoading = false
      this.hasError = false
    },
    handleError() {
      this.isLoading = false
      this.hasError = true
    }
  }
}
</script>

<style>
.image-container {
  position: relative;
}
.loading-placeholder,
.error-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>

这些实现方法可以根据具体需求进行组合或扩展,构建适合项目需求的图片组件。

标签: 组件图片
分享给朋友:

相关文章

vue组件实现

vue组件实现

Vue 组件实现 Vue 组件是 Vue.js 的核心特性之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种常见方式: 单文件组件(SFC) 单文件组件是 Vue 中…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现 Vue组件是Vue.js的核心概念之一,允许将UI拆分为独立可复用的代码片段。组件的实现方式有多种,以下是常见的几种方法。 单文件组件(SFC) 单文件组件以.vue为后缀,将…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性来完成。以下是几种常见的实…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

如何判断react组件

如何判断react组件

判断 React 组件的依据 React 组件的判断通常基于其定义方式、功能特性和代码结构。以下是几种常见的判断方法: 函数组件与类组件 函数组件是通过 JavaScript 函数定义的,接收 pr…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> &…