今天小编给大家分享一下Vue全局水印如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
【需求】系统内页面显示水印,登录页面没有水印(退出登录时,登录页面不会显示水印)
/* * @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 } // 该方法只允许调用一次 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 watermark
// 1.在App.vue文件中,导入该文件 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.在其他页面引用 import Watemark from "@/common/watermark"; created() { Watermark.set("admin") }
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("未获取到用户名") // 设置水印title } });
以上就是“Vue全局水印如何实现”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。