Vue3

vue项目禁止复制

2022-06-16  本文已影响0人  淡淡烟草味

在vue项目中禁止复制其实很简单,只需要加入以下代码既可:

一、js的方法,在生命周期函数中添加:

在vue 2.x中:
<template>
    <div>
        test
    </div>
</template>

<script>
export default {
    mounted() {
        // 禁用复制
        this.$nextTick(() => {
            document.onselectstart = new Function("event.returnValue=false");
        })
    },
}
</script>
在vue 3.0中:
<template>
    <div>
        test 2
    </div>
</template>

<script setup>
import { onMounted } from 'vue';
onMounted(() => {
    // 禁用复制
    document.onselectstart = new Function("event.returnValue=false");
})
</script>

<style scoped>
</style>

二、添加全局css(也可以添加在单个页面):

//全局选择
*{
user-select:none;
}
//或者部分标签
.test-class{
user-select:none;
}
上一篇下一篇

猜你喜欢

热点阅读