詳解Vue實現(xiàn)鏈接生成二維碼并支持下載
在現(xiàn)代 Web 應(yīng)用中,快速分享鏈接是一項常見需求。二維碼作為一種簡潔的分享方式,受到了廣泛歡迎。如何在 Vue.js 中實現(xiàn)前端生成鏈接二維碼的功能,成為了許多開發(fā)者關(guān)注的焦點。本文將介紹如何使用 Vue 純前端技術(shù)實現(xiàn)動態(tài)生成鏈接二維碼的方法,無需后端參與。
本項目基于Vite+Vue3,這里假設(shè)你已經(jīng)搭建好項目了前端頁面使用el-input+el-popover來實現(xiàn)
為了在應(yīng)用程序中實現(xiàn)鏈接生成二維碼的功能,我們需要依賴一個npm 包qrcode.vue。這個包提供了一個簡單而強大的方法,讓我們能夠輕松地在 Vue.js 應(yīng)用程序中生成二維碼,無需編寫冗長復(fù)雜的代碼,極大地簡化了開發(fā)流程,提高了開發(fā)效率。
具體屬性配置可以查看qrcode.vue官方文檔:github.com/scopewu/qrcode.vue
安裝
npm install --save qrcode.vue
在需要使用的文件中引入qrcode
<script setup>
import QrcodeVue from 'qrcode.vue'
const level = ref('M')
const renderAs = ref('svg')
import { ref } from 'vue'
const link = ref('https://www.mi.com/')
const onQRCode = () =>{}
</script>
<template>
<h1>前端實現(xiàn)鏈接生成二維碼</h1>
<el-input v-model="link" placeholder="Please input" style="width: 400px;">
<template #append>
<el-popover
placement="bottom"
:width="160"
trigger="click"
>
<template #reference>
<div class="qrcode">二維碼</div>
</template>
<template #default>
<div class="code-container">
<div class="title">掃描二維碼,分享此鏈接</div>
<qrcode-vue id="svgRef" :value="link" :size="120" :level="level" :render-as="renderAs" />
<el-button icon="sc-icon-ImportsIcon" style="width: 120px;margin-top: 10px;" class="m-t10" size="small" @click="downloadQrCode">下載</el-button>
</div>
</template>
</el-popover>
</template>
</el-input>
</template>
<style scoped>
.code-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.qrcode {
cursor: pointer;
}
.title {
font-size: 12px;
color: #8e939c;
margin-bottom: 5px;
}
</style>
效果如下

下載二維碼代碼實現(xiàn)
/* 下載二維碼 */
const downloadQrCode = () =>{
const node = document.getElementById('svgRef')
covertSVG2Image(node, '掃描二維碼,分享此鏈接', 120, 120)
};
/**
* 將svg導(dǎo)出成圖片
* @param node svg節(jié)點 => document.querySelector('svg')
* @param name 生成的圖片名稱
* @param width 生成的圖片寬度
* @param height 生成的圖片高度
* @param type 生成的圖片類型
*/
const covertSVG2Image = (node, name, width, height, type = 'png') => {
let serializer = new XMLSerializer()
let source = '<?xml version="1.0" standalone="no"?>\r\n' + serializer.serializeToString(node)
let image = new Image()
image.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(source)
let canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
let context = canvas.getContext('2d')
context.fillStyle = '#fff'
context.fillRect(0, 0, 10000, 10000)
image.onload = function () {
context.drawImage(image, 0, 0)
let a = document.createElement('a')
a.download = `${name}.${type}`
a.href = canvas.toDataURL(`image/${type}`)
a.click()
}
}
下載的二維碼

以上所述即是如何在前端實現(xiàn)二維碼生成以及支持下載的完整方案。
到此這篇關(guān)于詳解Vue實現(xiàn)鏈接生成二維碼并支持下載的文章就介紹到這了,更多相關(guān)Vue鏈接生成二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3+Ant?design?實現(xiàn)Select下拉框一鍵全選/清空功能
在做后臺管理系統(tǒng)項目的時候,產(chǎn)品增加了一個在Select選擇器中添加一鍵全選和清空的功能,他又不讓在外部增加按鈕,其實如果說在外部增加按鈕實現(xiàn)全選或者清空的話,功能比較簡單的,下面給大家分享Vue3+Ant?design?實現(xiàn)Select下拉框一鍵全選/清空功能,需要的朋友可以參考下2024-05-05
vue-cli-service和webpack-dev-server的區(qū)別及說明
這篇文章主要介紹了vue-cli-service和webpack-dev-server的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
詳解element-ui設(shè)置下拉選擇切換必填和非必填
這篇文章主要介紹了詳解element-ui設(shè)置下拉選擇切換必填和非必填,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-06-06
vue路由對不同界面進行傳參及跳轉(zhuǎn)的總結(jié)
這篇文章主要介紹了vue路由對不同界面進行傳參及跳轉(zhuǎn)的總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-04-04
想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法
這篇文章主要為大家介紹了想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

