
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
@ophiuchus/image
Advanced tools
增强版的 img 标签,提供多种图片填充模式,支持图片懒加载、加载中提示、加载失败提示。
import Vue from 'vue';
import Image from '@ophiuchus/image';
Vue.use(Image);
基础用法与原生 img 标签一致,可以设置 src、width、height、alt 等原生属性。
<sf-image
width="100"
height="100"
src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>
通过 fit 属性可以设置图片填充模式,可选值见下方表格。
<sf-image
width="10rem"
height="10rem"
fit="contain"
src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>
通过 round 属性可以设置图片变圆,注意当图片宽高不相等且 fit 为 contain 或 scale-down 时,将无法填充一个完整的圆形。
<sf-image
round
width="10rem"
height="10rem"
src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>
设置 lazy-load 属性来开启图片懒加载,需要搭配 Lazyload 组件使用。
<sf-image
width="100"
height="100"
lazy-load
src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg"
/>
import Vue from 'vue';
import Lazyload from '@ophiuchus/lazyload';
Vue.use(Lazyload);
Image 组件提供了默认的加载中提示,支持通过 loading 插槽自定义内容。
<sf-image src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg">
<template v-slot:loading>
<sf-loading type="spinner" size="20" />
</template>
</sf-image>
Image 组件提供了默认的加载失败提示,支持通过 error 插槽自定义内容。
<sf-image src="https://img3.tuhu.org/oySzSsdrSCR36UGcCtVbsQ_w800_h550.jpeg">
<template v-slot:error>加载失败</template>
</sf-image>
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| src | 图片链接 | string | - |
| fit | 图片填充模式 | string | fill |
| alt | 替代文本 | string | - |
| width | 宽度,默认单位为px | number | string | - |
| height | 高度,默认单位为px | number | string | - |
| radius | 圆角大小,默认单位为px | number | string | 0 |
| round | 是否显示为圆形 | boolean | false |
| lazy-load | 是否开启图片懒加载,须配合 Lazyload 组件使用 | boolean | false |
| show-error | 是否展示图片加载失败提示 | boolean | true |
| show-loading | 是否展示图片加载中提示 | boolean | true |
| error-icon | 失败时提示的图标名称或图片链接 | string | photo-fail |
| loading-icon | 加载时提示的图标名称或图片链接 | string | photo |
| icon-prefix | 图标类名前缀,同 Icon 组件的 class-prefix 属性 | string | sf-icon |
| 名称 | 含义 |
|---|---|
| contain | 保持宽高缩放图片,使图片的长边能完全显示出来 |
| cover | 保持宽高缩放图片,使图片的短边能完全显示出来,裁剪长边 |
| fill | 拉伸图片,使图片填满元素 |
| none | 保持图片原有尺寸 |
| scale-down | 取none或contain中较小的一个 |
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击图片时触发 | event: Event |
| load | 图片加载完毕时触发 | - |
| error | 图片加载失败时触发 | - |
| 名称 | 说明 |
|---|---|
| default | 自定义图片下方的内容 |
| loading | 自定义加载中的提示内容 |
| error | 自定义加载失败时的提示内容 |
组件提供了下列 Less 变量,可用于自定义样式,使用方法请参考主题定制。
| 名称 | 默认值 | 描述 |
|---|---|---|
| @image-placeholder-text-color | @gray-6 | - |
| @image-placeholder-font-size | @font-size-md | - |
| @image-placeholder-background-color | @background-color | - |
| @image-loading-icon-size | 32px | - |
| @image-loading-icon-color | @gray-4 | - |
| @image-error-icon-size | 32px | - |
| @image-error-icon-color | @gray-4 | - |
在 .vue 文件中通过相对路径引用本地图片时,需要在图片的链接外包上一层 require(),将图片 URL 转换为 webpack 模块请求,并结合 file-loader 或者 url-loader 进行处理。
<!-- 错误写法 -->
<sf-image src="./image.png" />
<!-- 正确写法 -->
<sf-image :src="__require__('./image.png')" /> // 由于md插件解析的bug,实际使用请把 __require__ 替换成 require。
对此更详细的解释可以参考 vue-loader 的处理资源路径章节。
使用 Image 组件时,可能会遇到将 <image> 作为标签名时无法渲染的问题,比如下面的写法:
<template>
<image src="xxx" />
</template>
<script>
import { Image } from '@ophiuchus/image';
export default {
components: {
Image,
},
};
<script>
这是因为 <image> 标签是原生的 SVG 标签,Vue 不允许将原生标签名注册为组件名,使用 <sf-image> 即可规避这个问题。
FAQs
### 介绍
The npm package @ophiuchus/image receives a total of 3 weekly downloads. As such, @ophiuchus/image popularity was classified as not popular.
We found that @ophiuchus/image demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.