feat: 🚀 封装打印指令

This commit is contained in:
bunny 2024-05-10 16:29:52 +08:00
parent 95a4af2b08
commit 176ac5971f
5 changed files with 38 additions and 9 deletions

View File

@ -1,9 +1,10 @@
import { App, Directive } from 'vue'; import { App, Directive } from 'vue';
import copy from './modules/copy'; import copy from './modules/copy';
import existShow from './modules/existShow'; import existShow from './modules/existShow';
import vPrint from './modules/print';
import waterMarker from './modules/waterMarker'; import waterMarker from './modules/waterMarker';
const directivesList: { [key: string]: Directive } = { copy, waterMarker, existShow }; const directivesList: { [key: string]: Directive } = { copy, waterMarker, existShow, vPrint };
/** /**
* *

View File

@ -1,16 +1,11 @@
import { Directive } from 'vue'; import { Directive } from 'vue';
interface ElType extends HTMLElement {
copyData: string | number; // 定义一个属性,用于存储需要复制的数据
__handleClick__: any; // 定义一个属性,用于存储事件处理函数
}
/** /**
* * 退 * * 退
*/ */
const existShow: Directive = { const existShow: Directive = {
// 指令与元素绑定时触发 // 指令与元素绑定时触发
beforeMount(el: ElType) { beforeMount() {
window.onbeforeunload = function (event) { window.onbeforeunload = function (event) {
(event || window.event).returnValue = '确定离开此页吗?'; (event || window.event).returnValue = '确定离开此页吗?';
}; };

View File

@ -0,0 +1,23 @@
import type { DirectiveBinding } from 'vue';
import { Directive } from 'vue';
interface ElType extends HTMLElement {
copyData: string | number; // 定义一个属性,用于存储需要复制的数据
__handleClick__: any; // 定义一个属性,用于存储事件处理函数
}
const vPrint: Directive = {
mounted(el: ElType, binding: DirectiveBinding) {
el.copyData = binding.value;
el.addEventListener('click', handelClick);
},
beforeMount(el: ElType) {
el.removeEventListener('click', el.__handleClick__);
},
};
function handelClick(this: any) {
print();
}
export default vPrint;

View File

@ -1,13 +1,19 @@
<template> <template>
<textarea type="text" v-model="myData.inputValue" /> <textarea type="text" v-model="myData.inputValue" />
<button v-copy="myData.inputValue">点击复制</button> <button v-copy="myData.copyValue" @click="handelClik">点击复制</button>
<button v-vPrint>点击打印</button>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { reactive } from 'vue'; import { reactive } from 'vue';
const myData = reactive({ const myData = reactive({
inputValue: '我是被复制的内容 🍒🍇🍊2023-4-21 14:02:59', inputValue: '我是被复制的内容 🍒🍇🍊2023-4-21 14:02:59',
copyValue: '',
}); });
const handelClik = () => {
myData.copyValue = myData.inputValue;
};
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
textarea { textarea {

View File

@ -1,6 +1,6 @@
<template> <template>
<div v-waterMarker="{ text: 'Bunny Admin', textColor: 'rgba(180, 180, 180, 0.6)' }" v-existShow> <div v-waterMarker="{ text: 'Bunny Admin', textColor: 'rgba(180, 180, 180, 0.6)' }" v-existShow>
<h1>bunny/default页面</h1> <h1 @click="handelClik">bunny/default页面</h1>
<My /> <My />
<RouterView /> <RouterView />
</div> </div>
@ -8,4 +8,8 @@
<script setup lang="ts"> <script setup lang="ts">
import My from '@/views/bunny/default/My.vue'; import My from '@/views/bunny/default/My.vue';
const handelClik = () => {
print();
};
</script> </script>