qrcode.vue 3.9.1 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README-zh_cn.md +51 -1
- package/README.md +51 -1
- package/README.zh-hant.md +360 -0
- package/dist/index.d.ts +7 -42
- package/dist/qrcode.vue.browser.js +52 -15
- package/dist/qrcode.vue.browser.min.js +2 -2
- package/dist/qrcode.vue.cjs.js +52 -15
- package/dist/qrcode.vue.esm.js +53 -16
- package/package.json +10 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [3.10.0] - 2026-06-13
|
|
2
|
+
|
|
3
|
+
### Feature
|
|
4
|
+
|
|
5
|
+
- Add `crossOrigin` support to `imageSettings` for both Canvas and SVG logo images.
|
|
6
|
+
- Expose `toDataURL` and `download` methods via template ref on `QrcodeCanvas`.
|
|
7
|
+
- Expose `toDataURL` and `download` methods via template ref on `QrcodeSvg`.
|
|
8
|
+
- `QrcodeVue` now forwards the above template ref methods based on `render-as`.
|
|
9
|
+
|
|
10
|
+
### Bugfix
|
|
11
|
+
|
|
12
|
+
- Remove `aria-label` from rendered output to avoid leaking encoded `value`.
|
|
13
|
+
- Fix declaration output path in Rollup build (`dist/src/index.d.ts` → `dist/index.d.ts`).
|
|
14
|
+
|
|
1
15
|
## [3.9.1]
|
|
2
16
|
|
|
3
17
|
### Bugfix
|
package/README-zh_cn.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
🔒 如果你正在使用 Vue 2,请保持 `qrcode.vue` 的版本为 `1.x`;
|
|
6
6
|
|
|
7
|
-
一款 Vue.js
|
|
7
|
+
一款 Vue.js 二维码组件,同时支援 Vue 2 和 Vue 3.
|
|
8
8
|
|
|
9
9
|
[](https://github.com/scopewu/qrcode.vue/blob/main/LICENSE)
|
|
10
10
|
|
|
@@ -104,6 +104,7 @@ createApp({
|
|
|
104
104
|
// x: 10,
|
|
105
105
|
// y: 10,
|
|
106
106
|
excavate: true,
|
|
107
|
+
// crossOrigin: 'anonymous', // 如需把 canvas 导出为图片,请设置此项。
|
|
107
108
|
})
|
|
108
109
|
|
|
109
110
|
// 可传入渐变相关的属性,支持渐变:
|
|
@@ -184,6 +185,7 @@ createApp({
|
|
|
184
185
|
// 使用此选项可确保图像周围的边缘清晰。嵌入透明图像时也很有用。
|
|
185
186
|
excavate?: boolean,
|
|
186
187
|
borderRadius?: number, // 图片的边框圆角。
|
|
188
|
+
crossOrigin?: 'anonymous' | 'use-credentials' | '', // 图片的 CORS 属性。如需导出 canvas 为图片,请设置此项。
|
|
187
189
|
}
|
|
188
190
|
```
|
|
189
191
|
|
|
@@ -262,6 +264,54 @@ createApp({
|
|
|
262
264
|
|
|
263
265
|
传递给二维码根元素的类名。
|
|
264
266
|
|
|
267
|
+
## 模板 Ref 方法(`QrcodeCanvas` / `QrcodeSvg`)
|
|
268
|
+
|
|
269
|
+
`QrcodeCanvas` 和 `QrcodeSvg` 都通过模板 ref 暴露以下方法。`QrcodeVue` 会根据当前的 `render-as` 转发这些方法。
|
|
270
|
+
|
|
271
|
+
### `QrcodeCanvas`
|
|
272
|
+
|
|
273
|
+
```html
|
|
274
|
+
<script setup>
|
|
275
|
+
import { ref } from 'vue'
|
|
276
|
+
import { QrcodeCanvas } from 'qrcode.vue'
|
|
277
|
+
|
|
278
|
+
const qrRef = ref()
|
|
279
|
+
const handleDownload = () => {
|
|
280
|
+
qrRef.value?.download('my-qrcode.png')
|
|
281
|
+
}
|
|
282
|
+
</script>
|
|
283
|
+
|
|
284
|
+
<qrcode-canvas ref="qrRef" value="https://example.com" />
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
| 方法 | 签名 | 说明 |
|
|
288
|
+
|------|------|------|
|
|
289
|
+
| `toDataURL` | `(type?: string, quality?: number) => string \| undefined` | 将 canvas 转换为 Data URL。 |
|
|
290
|
+
| `download` | `(filename?: string) => void` | 触发下载二维码 PNG 图片。 |
|
|
291
|
+
|
|
292
|
+
> **CORS 注意:** 当二维码包含跨域 Logo 图片时,请确保设置 `imageSettings.crossOrigin: 'anonymous'`,且图片服务器返回 `Access-Control-Allow-Origin` 响应头。否则 canvas 会被"污染",导致 `toDataURL` / `download` 抛出 `SecurityError`。
|
|
293
|
+
|
|
294
|
+
### `QrcodeSvg`
|
|
295
|
+
|
|
296
|
+
```html
|
|
297
|
+
<script setup>
|
|
298
|
+
import { ref } from 'vue'
|
|
299
|
+
import { QrcodeSvg } from 'qrcode.vue'
|
|
300
|
+
|
|
301
|
+
const qrRef = ref()
|
|
302
|
+
const handleDownload = () => {
|
|
303
|
+
qrRef.value?.download('my-qrcode.svg')
|
|
304
|
+
}
|
|
305
|
+
</script>
|
|
306
|
+
|
|
307
|
+
<qrcode-svg ref="qrRef" value="https://example.com" />
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
| 方法 | 签名 | 说明 |
|
|
311
|
+
|------|------|------|
|
|
312
|
+
| `toDataURL` | `() => string \| undefined` | 将 SVG 元素转换为 Data URL。 |
|
|
313
|
+
| `download` | `(filename?: string) => void` | 触发下载二维码 SVG 图片。 |
|
|
314
|
+
|
|
265
315
|
## `QrcodeVue` 3.5+
|
|
266
316
|
|
|
267
317
|
`QrcodeVue` 3.5+ 后导出独立的 `QrcodeCanvas` 和 `QrcodeSvg` 组件,为此修改了 rollup 的配置:
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
A Vue.js component to generate [QRCode](https://en.wikipedia.org/wiki/QR_code). Both support Vue 2 and Vue 3.
|
|
8
8
|
|
|
9
|
-
[
|
|
9
|
+
[简体中文](./README-zh_cn.md) | [繁體中文](./README.zh-hant.md) | [日本語](./README-ja.md)
|
|
10
10
|
|
|
11
11
|
## install
|
|
12
12
|
|
|
@@ -107,6 +107,7 @@ When you use the component with Vue 3 with `TypeScript`:
|
|
|
107
107
|
// x: 10,
|
|
108
108
|
// y: 10,
|
|
109
109
|
excavate: true,
|
|
110
|
+
// crossOrigin: 'anonymous', // Set this when you need to export the canvas to an image.
|
|
110
111
|
})
|
|
111
112
|
|
|
112
113
|
const gradient = ref(false)
|
|
@@ -182,6 +183,7 @@ The foreground color of qrcode.
|
|
|
182
183
|
width: number, // The height of image
|
|
183
184
|
excavate?: boolean, // Whether or not to "excavate" the modules around the image.
|
|
184
185
|
borderRadius?: number, // The border radius of image.
|
|
186
|
+
crossOrigin?: 'anonymous' | 'use-credentials' | '', // The CORS attribute for the image. Useful when exporting canvas to an image.
|
|
185
187
|
}
|
|
186
188
|
```
|
|
187
189
|
|
|
@@ -260,6 +262,54 @@ The end color of the gradient.
|
|
|
260
262
|
|
|
261
263
|
The class name of qrcode element.
|
|
262
264
|
|
|
265
|
+
## Template Ref Methods (`QrcodeCanvas` / `QrcodeSvg`)
|
|
266
|
+
|
|
267
|
+
Both `QrcodeCanvas` and `QrcodeSvg` expose the following methods via template ref. `QrcodeVue` forwards them based on the current `render-as`.
|
|
268
|
+
|
|
269
|
+
### `QrcodeCanvas`
|
|
270
|
+
|
|
271
|
+
```html
|
|
272
|
+
<script setup>
|
|
273
|
+
import { ref } from 'vue'
|
|
274
|
+
import { QrcodeCanvas } from 'qrcode.vue'
|
|
275
|
+
|
|
276
|
+
const qrRef = ref()
|
|
277
|
+
const handleDownload = () => {
|
|
278
|
+
qrRef.value?.download('my-qrcode.png')
|
|
279
|
+
}
|
|
280
|
+
</script>
|
|
281
|
+
|
|
282
|
+
<qrcode-canvas ref="qrRef" value="https://example.com" />
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
| Method | Signature | Description |
|
|
286
|
+
|--------|-----------|-------------|
|
|
287
|
+
| `toDataURL` | `(type?: string, quality?: number) => string \| undefined` | Convert the canvas to a data URL. |
|
|
288
|
+
| `download` | `(filename?: string) => void` | Trigger a download of the QR code as a PNG image. |
|
|
289
|
+
|
|
290
|
+
> **CORS note:** When the QR code includes a cross-origin logo image, make sure to set `imageSettings.crossOrigin: 'anonymous'` and that the image server responds with the `Access-Control-Allow-Origin` header. Otherwise the canvas becomes "tainted" and `toDataURL` / `download` will throw a `SecurityError`.
|
|
291
|
+
|
|
292
|
+
### `QrcodeSvg`
|
|
293
|
+
|
|
294
|
+
```html
|
|
295
|
+
<script setup>
|
|
296
|
+
import { ref } from 'vue'
|
|
297
|
+
import { QrcodeSvg } from 'qrcode.vue'
|
|
298
|
+
|
|
299
|
+
const qrRef = ref()
|
|
300
|
+
const handleDownload = () => {
|
|
301
|
+
qrRef.value?.download('my-qrcode.svg')
|
|
302
|
+
}
|
|
303
|
+
</script>
|
|
304
|
+
|
|
305
|
+
<qrcode-svg ref="qrRef" value="https://example.com" />
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
| Method | Signature | Description |
|
|
309
|
+
|--------|-----------|-------------|
|
|
310
|
+
| `toDataURL` | `() => string \| undefined` | Convert the SVG element to a data URL. |
|
|
311
|
+
| `download` | `(filename?: string) => void` | Trigger a download of the QR code as an SVG image. |
|
|
312
|
+
|
|
263
313
|
## `QrcodeVue` 3.5+
|
|
264
314
|
|
|
265
315
|
`QrcodeVue` 3.5+ exports separate `QrcodeCanvas` and `QrcodeSvg` components, for which the rollup configuration has been modified:
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
# qrcode.vue
|
|
2
|
+
|
|
3
|
+
⚠️ 如果你正在使用 Vue 3,請升級 `qrcode.vue` 到 `3.x`;
|
|
4
|
+
|
|
5
|
+
🔒 如果你正在使用 Vue 2,請保持 `qrcode.vue` 的版本爲 `1.x`;
|
|
6
|
+
|
|
7
|
+
一款 Vue.js 二維碼組件(QR Code),同時支援 Vue 2 和 Vue 3.
|
|
8
|
+
|
|
9
|
+
[](https://github.com/scopewu/qrcode.vue/blob/main/LICENSE)
|
|
10
|
+
|
|
11
|
+
## 快速開始
|
|
12
|
+
|
|
13
|
+
快速添加 `qrcode.vue` 組件到項目中
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install --save qrcode.vue # yarn add qrcode.vue
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
dist/
|
|
21
|
+
|--- qrcode.vue.cjs.js // CommonJS
|
|
22
|
+
|--- qrcode.vue.esm.js // ES module
|
|
23
|
+
|--- qrcode.vue.browser.js // UMD for browser or require.js or CommonJS
|
|
24
|
+
|--- qrcode.vue.browser.min.js // UMD Minimum size
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 使用
|
|
28
|
+
|
|
29
|
+
e.g.
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { createApp } from 'vue'
|
|
33
|
+
import QrcodeVue from 'qrcode.vue'
|
|
34
|
+
|
|
35
|
+
createApp({
|
|
36
|
+
data: {
|
|
37
|
+
value: 'https://example.com',
|
|
38
|
+
},
|
|
39
|
+
template: '<qrcode-vue :value="value"></qrcode-vue>',
|
|
40
|
+
components: {
|
|
41
|
+
QrcodeVue,
|
|
42
|
+
},
|
|
43
|
+
}).mount('#root')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
或者,在獨有單文件擴展 `*.vue` 中使用:
|
|
47
|
+
|
|
48
|
+
```html
|
|
49
|
+
<template>
|
|
50
|
+
<qrcode-vue :value="value" :size="size" level="H" />
|
|
51
|
+
</template>
|
|
52
|
+
<script>
|
|
53
|
+
import QrcodeVue from 'qrcode.vue'
|
|
54
|
+
|
|
55
|
+
export default {
|
|
56
|
+
data() {
|
|
57
|
+
return {
|
|
58
|
+
value: 'https://example.com',
|
|
59
|
+
size: 300,
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
components: {
|
|
63
|
+
QrcodeVue,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
</script>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
在 Vue 3 中配合 `TypeScript` 使用:
|
|
70
|
+
|
|
71
|
+
```html
|
|
72
|
+
<template>
|
|
73
|
+
<qrcode-vue
|
|
74
|
+
:value="value"
|
|
75
|
+
:level="level"
|
|
76
|
+
:render-as="renderAs"
|
|
77
|
+
:background="background"
|
|
78
|
+
:foreground='foreground'
|
|
79
|
+
:gradient="gradient"
|
|
80
|
+
:gradient-type="gradientType"
|
|
81
|
+
:gradient-start-color="gradientStartColor"
|
|
82
|
+
:gradient-end-color="gradientEndColor"
|
|
83
|
+
:image-settings='imageSettings'
|
|
84
|
+
:radius="radius"
|
|
85
|
+
/>
|
|
86
|
+
</template>
|
|
87
|
+
<script setup lang="ts">
|
|
88
|
+
import { ref } from 'vue'
|
|
89
|
+
import QrcodeVue from 'qrcode.vue'
|
|
90
|
+
import type { Level, RenderAs, GradientType, ImageSettings } from 'qrcode.vue'
|
|
91
|
+
|
|
92
|
+
const value = ref('qrcode')
|
|
93
|
+
const level = ref<Level>('M')
|
|
94
|
+
const renderAs = ref<RenderAs>('svg')
|
|
95
|
+
const background = ref('#ffffff')
|
|
96
|
+
const foreground = ref('#000000')
|
|
97
|
+
const margin = ref(0)
|
|
98
|
+
|
|
99
|
+
// 可傳入二維碼圖片相關的屬性,支持二維碼 LOGO;
|
|
100
|
+
const imageSettings = ref<ImageSettings>({
|
|
101
|
+
src: 'https://github.com/scopewu.png',
|
|
102
|
+
width: 30,
|
|
103
|
+
height: 30,
|
|
104
|
+
// x: 10,
|
|
105
|
+
// y: 10,
|
|
106
|
+
excavate: true,
|
|
107
|
+
// crossOrigin: 'anonymous', // 如需把 canvas 導出為圖片,請設置此項。
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// 可傳入漸變相關的屬性,支持漸變:
|
|
111
|
+
const gradient = ref(false)
|
|
112
|
+
const gradientType = ref<GradientType>('linear')
|
|
113
|
+
const gradientStartColor = ref('#000000')
|
|
114
|
+
const gradientEndColor = ref('#38bdf8')
|
|
115
|
+
// 可傳入圓角半徑:
|
|
116
|
+
const radius = ref(0)
|
|
117
|
+
</script>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Component props
|
|
121
|
+
|
|
122
|
+
### `value`
|
|
123
|
+
|
|
124
|
+
- 類型:`string`
|
|
125
|
+
- 默認值:`''`
|
|
126
|
+
|
|
127
|
+
二維碼的內容值。
|
|
128
|
+
|
|
129
|
+
### `size`
|
|
130
|
+
|
|
131
|
+
- 類型:`number`
|
|
132
|
+
- 默認值:`100`
|
|
133
|
+
|
|
134
|
+
二維碼大小。
|
|
135
|
+
|
|
136
|
+
### `render-as`
|
|
137
|
+
|
|
138
|
+
- 類型:`RenderAs('canvas' | 'svg')`
|
|
139
|
+
- 默認值:`canvas`
|
|
140
|
+
|
|
141
|
+
生成二維碼的 HTML 標籤,可選 `canvas` 或 `svg`。其中 `svg` 可以用於 SSR。
|
|
142
|
+
|
|
143
|
+
### `margin`
|
|
144
|
+
|
|
145
|
+
- 類型:`number`
|
|
146
|
+
- 默認值:`0`
|
|
147
|
+
|
|
148
|
+
定義空白區的寬度應該是多少。
|
|
149
|
+
|
|
150
|
+
### `level`
|
|
151
|
+
|
|
152
|
+
- 類型:`Level('L' | 'M' | 'Q' | 'H')`
|
|
153
|
+
- 默認值:`L`
|
|
154
|
+
|
|
155
|
+
二維碼的容錯能力等級,取值爲 'L', 'M', 'Q', 'H' 之一。瞭解更多,[維基百科:QR_code](https://en.wikipedia.org/wiki/QR_code#Error_correction)。
|
|
156
|
+
|
|
157
|
+
### `background`
|
|
158
|
+
|
|
159
|
+
- 類型:`string`
|
|
160
|
+
- 默認值:`#ffffff`
|
|
161
|
+
|
|
162
|
+
二維碼背景顏色。
|
|
163
|
+
|
|
164
|
+
### `foreground`
|
|
165
|
+
|
|
166
|
+
- 類型:`string`
|
|
167
|
+
- 默認值:`#000000`
|
|
168
|
+
|
|
169
|
+
二維碼前景顏色。
|
|
170
|
+
|
|
171
|
+
### `image-settings`
|
|
172
|
+
|
|
173
|
+
- 類型: `ImageSettings`
|
|
174
|
+
- 默認值: `{}`
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
export type ImageSettings = {
|
|
178
|
+
src: string, // 圖片的地址。
|
|
179
|
+
x?: number, // 水平橫向偏移。沒有設定值時,圖片劇中
|
|
180
|
+
y?: number, // 垂直豎向偏移。沒有設定值時,圖片劇中
|
|
181
|
+
height: number, // 圖片的高度
|
|
182
|
+
width: number, // 圖片的寬度
|
|
183
|
+
// 是否“挖掘”圖像周圍的模塊。
|
|
184
|
+
// 這意味着嵌入圖像重疊的任何模塊都將使用背景顏色。
|
|
185
|
+
// 使用此選項可確保圖像周圍的邊緣清晰。嵌入透明圖像時也很有用。
|
|
186
|
+
excavate?: boolean,
|
|
187
|
+
borderRadius?: number, // 圖片的邊框圓角。
|
|
188
|
+
crossOrigin?: 'anonymous' | 'use-credentials' | '', // 圖片的 CORS 屬性。如需導出 canvas 為圖片,請設置此項。
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
二維碼圖片 logo 配置。
|
|
193
|
+
|
|
194
|
+
### `radius`
|
|
195
|
+
|
|
196
|
+
- 類型:`number`
|
|
197
|
+
- 默認值:`0`
|
|
198
|
+
|
|
199
|
+
每個二維碼模塊的圓角半徑,相對於模塊寬度的比例。接受 `0` 到 `0.5` 的值。
|
|
200
|
+
|
|
201
|
+
- `0`(默認)- 方形模塊,直角
|
|
202
|
+
- `0.5` - 最大圓角,模塊變爲圓形
|
|
203
|
+
|
|
204
|
+
圓角是上下文感知的:相鄰深色模塊之間的內角保持直角,外角則圓角化。
|
|
205
|
+
|
|
206
|
+
```html
|
|
207
|
+
<qrcode-vue value="test" :radius="0.35" />
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### `id`
|
|
211
|
+
|
|
212
|
+
- 類型:`string`
|
|
213
|
+
- 默認值:`undefined`
|
|
214
|
+
|
|
215
|
+
自定義 SVG 內部元素(漸變、裁剪路徑)的 ID。SSR 水合一致性需要此屬性 — 傳入 `useId()` 以確保服務端和客戶端生成匹配的 ID。
|
|
216
|
+
|
|
217
|
+
```html
|
|
218
|
+
<script setup>
|
|
219
|
+
// 僅 vue 3.5+
|
|
220
|
+
import { useId } from 'vue'
|
|
221
|
+
const uid = useId()
|
|
222
|
+
|
|
223
|
+
// vue 3.4 及以下版本
|
|
224
|
+
// const uid = 'qrcode-1'
|
|
225
|
+
// const uid2 = 'qrcode-2'
|
|
226
|
+
</script>
|
|
227
|
+
<qrcode-svg value="test" :id="uid" render-as="svg" />
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
省略時使用模塊級計數器,該計數器在多請求的 SSR 環境中不安全。
|
|
231
|
+
|
|
232
|
+
### `gradient`
|
|
233
|
+
|
|
234
|
+
- 類型: `boolean`
|
|
235
|
+
- 默認值: `false`
|
|
236
|
+
|
|
237
|
+
啓用二維碼的漸變填充。
|
|
238
|
+
|
|
239
|
+
### `gradient-type`
|
|
240
|
+
|
|
241
|
+
- 類型: `GradientType('linear' | 'radial')`
|
|
242
|
+
- 默認值: `linear`
|
|
243
|
+
|
|
244
|
+
指定漸變類型。
|
|
245
|
+
|
|
246
|
+
### `gradient-start-color`
|
|
247
|
+
|
|
248
|
+
- 類型: `string`
|
|
249
|
+
- 默認值: `#000000`
|
|
250
|
+
|
|
251
|
+
漸變的起始顏色。
|
|
252
|
+
|
|
253
|
+
### `gradient-end-color`
|
|
254
|
+
|
|
255
|
+
- 類型: `string`
|
|
256
|
+
- 默認值: `#ffffff`
|
|
257
|
+
|
|
258
|
+
漸變的結束顏色。
|
|
259
|
+
|
|
260
|
+
### `class`
|
|
261
|
+
|
|
262
|
+
- 類型:`string`
|
|
263
|
+
- 默認值:`''`
|
|
264
|
+
|
|
265
|
+
傳遞給二維碼根元素的類名。
|
|
266
|
+
|
|
267
|
+
## 模板 Ref 方法(`QrcodeCanvas` / `QrcodeSvg`)
|
|
268
|
+
|
|
269
|
+
`QrcodeCanvas` 與 `QrcodeSvg` 都透過模板 ref 暴露以下方法。`QrcodeVue` 會根據目前的 `render-as` 轉發這些方法。
|
|
270
|
+
|
|
271
|
+
### `QrcodeCanvas`
|
|
272
|
+
|
|
273
|
+
```html
|
|
274
|
+
<script setup>
|
|
275
|
+
import { ref } from 'vue'
|
|
276
|
+
import { QrcodeCanvas } from 'qrcode.vue'
|
|
277
|
+
|
|
278
|
+
const qrRef = ref()
|
|
279
|
+
const handleDownload = () => {
|
|
280
|
+
qrRef.value?.download('my-qrcode.png')
|
|
281
|
+
}
|
|
282
|
+
</script>
|
|
283
|
+
|
|
284
|
+
<qrcode-canvas ref="qrRef" value="https://example.com" />
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
| 方法 | 簽名 | 說明 |
|
|
288
|
+
|------|------|------|
|
|
289
|
+
| `toDataURL` | `(type?: string, quality?: number) => string \| undefined` | 將 canvas 轉換為 Data URL。 |
|
|
290
|
+
| `download` | `(filename?: string) => void` | 觸發下載二維碼 PNG 圖片。 |
|
|
291
|
+
|
|
292
|
+
> **CORS 注意:** 當二維碼包含跨域 Logo 圖片時,請確保設置 `imageSettings.crossOrigin: 'anonymous'`,且圖片伺服器返回 `Access-Control-Allow-Origin` 響應頭。否則 canvas 會被「污染」,導致 `toDataURL` / `download` 拋出 `SecurityError`。
|
|
293
|
+
|
|
294
|
+
### `QrcodeSvg`
|
|
295
|
+
|
|
296
|
+
```html
|
|
297
|
+
<script setup>
|
|
298
|
+
import { ref } from 'vue'
|
|
299
|
+
import { QrcodeSvg } from 'qrcode.vue'
|
|
300
|
+
|
|
301
|
+
const qrRef = ref()
|
|
302
|
+
const handleDownload = () => {
|
|
303
|
+
qrRef.value?.download('my-qrcode.svg')
|
|
304
|
+
}
|
|
305
|
+
</script>
|
|
306
|
+
|
|
307
|
+
<qrcode-svg ref="qrRef" value="https://example.com" />
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
| 方法 | 簽名 | 說明 |
|
|
311
|
+
|------|------|------|
|
|
312
|
+
| `toDataURL` | `() => string \| undefined` | 將 SVG 元素轉換為 Data URL。 |
|
|
313
|
+
| `download` | `(filename?: string) => void` | 觸發下載二維碼 SVG 圖片。 |
|
|
314
|
+
|
|
315
|
+
## `QrcodeVue` 3.5+
|
|
316
|
+
|
|
317
|
+
`QrcodeVue` 3.5+ 後導出獨立的 `QrcodeCanvas` 和 `QrcodeSvg` 組件,爲此修改了 rollup 的配置:
|
|
318
|
+
|
|
319
|
+
```
|
|
320
|
+
// rollup.config.js
|
|
321
|
+
|
|
322
|
+
- exports: 'default',
|
|
323
|
+
+ exports: 'named',
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
現在在 common.js 和 cdn 直接引用 `QrcodeVue` 需要使用 `default` 字段:
|
|
327
|
+
|
|
328
|
+
```js
|
|
329
|
+
const QrcodeVue = require('qrcode.vue').default
|
|
330
|
+
const { default: QrcodeVue, QrcodeCanvas, QrcodeSvg } = require('qrcode.vue')
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
```html
|
|
334
|
+
<!--With HTML-->
|
|
335
|
+
<div id="root">
|
|
336
|
+
<p class="flex space-x">
|
|
337
|
+
<qrcode-vue :value="test" render-as="svg"></qrcode-vue>
|
|
338
|
+
<qrcode-canvas :value="test"></qrcode-canvas>
|
|
339
|
+
</p>
|
|
340
|
+
<p><input v-model="test" /></p>
|
|
341
|
+
</div>
|
|
342
|
+
<script src="https://cdn.jsdelivr.net/npm/vue@3.5/dist/vue.global.prod.js"></script>
|
|
343
|
+
<script src="https://cdn.jsdelivr.net/npm/qrcode.vue@3.5/dist/qrcode.vue.browser.min.js"></script>
|
|
344
|
+
|
|
345
|
+
<script>
|
|
346
|
+
Vue.createApp({
|
|
347
|
+
data() { return {
|
|
348
|
+
test: 'Hello World',
|
|
349
|
+
}},
|
|
350
|
+
components: {
|
|
351
|
+
QrcodeVue: QrcodeVue.default,
|
|
352
|
+
QrcodeCanvas: QrcodeVue.QrcodeCanvas,
|
|
353
|
+
},
|
|
354
|
+
}).mount('#root')
|
|
355
|
+
</script>
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## 軟件許可
|
|
359
|
+
|
|
360
|
+
copyright © 2021 scopewu, license by [MIT](https://github.com/scopewu/qrcode.vue/blob/main/LICENSE)
|