Vue之全局水印的實(shí)現(xiàn)示例
【需求】系統(tǒng)內(nèi)頁(yè)面顯示水印,登錄頁(yè)面沒(méi)有水印(退出登錄時(shí),登錄頁(yè)面不會(huì)顯示水印)
1.創(chuàng)建水印Js文件/* * @Author: 劉小二 * @Date: 2021-07-15 14:43:27 * @LastEditTime: 2021-07-15 15:00:27 * @LastEditors: Please set LastEditors * @Description: 添加水印 * @FilePath: /huashijc_MeetingSys/src/common/warterMark.js */’use strict’ let watermark = {} let setWatermark = (str) => { let id = ’1.23452384164.123412415’ if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)) } let can = document.createElement(’canvas’) can.width = 250 can.height = 120 let cans = can.getContext(’2d’) cans.rotate(-15 * Math.PI / 150) cans.font = ’20px Vedana’ cans.fillStyle = ’rgba(200, 200, 200, 0.20)’ cans.textAlign = ’left’ cans.textBaseline = ’Middle’ cans.fillText(str, can.width / 8, can.height / 2) let div = document.createElement(’div’) div.id = id div.style.pointerEvents = ’none’ div.style.top = ’35px’ div.style.left = ’0px’ div.style.position = ’fixed’ div.style.zIndex = ’100000’ div.style.width = document.documentElement.clientWidth + ’px’ div.style.height = document.documentElement.clientHeight + ’px’ div.style.background = ’url(’ + can.toDataURL(’image/png’) + ’) left top repeat’ document.body.appendChild(div) return id} // 該方法只允許調(diào)用一次watermark.set = (str) => { let id = setWatermark(str) setInterval(() => { if (document.getElementById(id) === null) { id = setWatermark(str) } }, 500) window.onresize = () => { setWatermark(str) }}const outWatermark = (id) => { if (document.getElementById(id) !== null) { const div = document.getElementById(id) div.style.display = ’none’ }}watermark.out = () => { const str = ’1.23452384164.123412415’ outWatermark(str)} export default watermark2.引入操作2.1 在App.vue中引用或其他頁(yè)面
// 1.在App.vue文件中,導(dǎo)入該文件import Watemark from ’@/common/watermark’;computed: { userName() { const name = this.$store.state.user.name return (name && name.length > 0) ? name : ’未獲取到用戶名’ }},mounted() { Watermark.set(this.userName)}// 2.在其他頁(yè)面引用import Watemark from ’@/common/watermark’;created() { Watermark.set(’admin’)}2.2 在router配置文件中引用
const outWatermark = (id) => { if (document.getElementById(id) !== null) { const div = document.getElementById(id) div.style.display = ’none’ }}router.afterEach((to) => { if(to.path == ’/’){ Watermark.out() // 清除水印 }else{ Watermark.set(’未獲取到用戶名’) // 設(shè)置水印title }});
到此這篇關(guān)于Vue之全局水印的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Vue 全局水印內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. 理解PHP5中static和const關(guān)鍵字3. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. IntelliJ IDEA安裝插件的方法步驟6. phpstorm恢復(fù)默認(rèn)設(shè)置的方法步驟7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Python random庫(kù)使用方法及異常處理方案9. Vuex localStorage的具體使用10. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟

網(wǎng)公網(wǎng)安備