Skip to content

基础二维码 (xh-qrcode)

基础二维码组件是最核心的组件,可用于生成标准的二维码。

基本用法

html
<xh-qrcode value="Hello World!"></xh-qrcode>

属性 (Attributes)

属性名类型默认值描述
valuestring''二维码内容
versionnumberundefined二维码版本 (1-40)
errorcorrectionlevelstring'M'错误纠正级别 ('L', 'M', 'Q', 'H')
pixelSizenumber4像素大小
colorstring'#000000'像素颜色
backgroundstring'#ffffff'背景颜色
paddingnumberundefined内边距
sizenumberundefined二维码大小
logostringundefinedLogo 图片 URL
logoScalenumberundefinedLogo 大小比例 (0-1)
logoPaddingnumber0.1Logo 内边距 (0-1)
maskbooleanfalse是否显示遮罩层
maskColorstring'rgba(255, 255, 255, 0.9)'遮罩层颜色
resizeRedrawbooleantrue窗口大小改变时是否重新绘制
shapestring'normal'像素形状 ('normal', 'circle', 'round')

示例

基础二维码

html
<xh-qrcode value="Basic QR Code"></xh-qrcode>

自定义颜色

html
<xh-qrcode value="Colored QR Code" color="#00ff00" background="#000000"></xh-qrcode>

自定义大小

html
<xh-qrcode value="Sized QR Code" size="200"></xh-qrcode>

高纠错级别

html
<xh-qrcode value="High Error Correction" errorcorrectionlevel="H"></xh-qrcode>
html
<xh-qrcode value="With Logo" errorcorrectionlevel="H" logo="./logo.svg"></xh-qrcode>

自定义 Logo 大小

html
<xh-qrcode
  value="Large Logo"
  errorcorrectionlevel="H"
  logo="./logo.svg"
  logoScale="0.3"
></xh-qrcode>

显示遮罩层

html
<xh-qrcode value="Masked QR Code" mask></xh-qrcode>

自定义遮罩层颜色

html
<xh-qrcode value="Custom Mask Color" mask maskColor="rgba(0, 0, 0, 0.8)"></xh-qrcode>

带遮罩层内容

html
<xh-qrcode value="With Mask Content" mask>
  <span style="background: pink; padding: 4px; border-radius: 4px">Mask Content</span>
</xh-qrcode>
Mask Content

不同形状

html
<!-- 圆点形状 -->
<xh-qrcode value="Circle Shape" shape="circle"></xh-qrcode>

<!-- 圆角方形 -->
<xh-qrcode value="Round Shape" shape="round"></xh-qrcode>

CSS 自定义属性

可通过 CSS 自定义属性修改组件样式:

属性名默认值描述
--mask-colorrgba(255, 255, 255, 0.9)遮罩层颜色

示例:

html
<style>
  .custom-qrcode {
    --mask-color: rgba(0, 0, 0, 0.8);
  }
</style>

<xh-qrcode class="custom-qrcode" value="Custom Style" mask></xh-qrcode>

事件

组件会派发以下事件:

事件名描述
error当生成二维码过程中发生错误时触发

监听错误事件:

html
<xh-qrcode value="Test" @error="handleError"></xh-qrcode>

<script>
  function handleError(event) {
    console.error('生成二维码失败:', event.detail)
  }
</script>