What are vue custom directives? Please moveVue custom commands
The implemented instructions are compatible with browsers that do not support API, and achieve the effect of throttling. The default interval is 1000ms.
1. Create a file, such as:
import { notification } from 'ant-design-vue' // Customize some propertiesinterface IListenter { (event: MouseEvent): void } interface ICopyElement extends HTMLElement { $value: string $isSupported: boolean $isClick: boolean $timer: number $handleCopy: IListenter } const useCopy = (app: ReturnType<typeof createApp>) => { ('copy', { mounted(el: ICopyElement, binding: ReturnType<typeof Object>) { (binding) let timer = ?.split(':')[0] || '' // Determine whether the timer exists and is a number. If it is not a number, the default value is assigned 1000ms if (timer && parseInt(timer) != timer) { el.$timer = parseInt(timer) } else { el.$timer = 1000 } el.$value = el.$handleCopy = async (event: MouseEvent) => { // Simply make a severity if (el.$isClick) return el.$isClick = true let t = setTimeout(() => { clearTimeout(t) el.$isClick = false }, el.$timer) if (!el.$value) { // When the value is empty, give a prompt ({ message: 'System prompt', description: 'No copy content' }) return } // Get whether copy API is supported if (el.$isSupported === undefined) { el.$isSupported = navigator && 'clipboard' in navigator } // Determine whether the browser supports it if (!el.$isSupported) { // Not supported, use the old copy method // Dynamically create textarea tags const textarea = ('textarea') // Set the textarea to readonly to prevent the keyboard from being automatically evoked under iOS, and to move the textarea out of the visual area = true = 'fixed' = '-9999px' // Assign the value to copy to the value attribute of the textarea tag = el.$value // Insert textarea into body (textarea) // Select the value and copy () // (0, ); const result = ('Copy') if (result) { ({ message: 'System prompt', description: 'Copy successfully' }) } (textarea) } else { // Use clipboard API await navigator!.(el.$value) ({ message: 'System prompt', description: 'Copy successfully' }) } } ('click', el.$handleCopy, false) }, unmounted(el: ICopyElement) { ('click', el.$handleCopy) } }) } export default (app: ReturnType<typeof createApp>) => { useCopy(app) }
2. Use in the file
import App from './' import * as copyFn from './copy' // The file created aboveconst app = createApp(App) if (typeof === 'function') { (app) } ('#app')
The above writing method can be changed according to the situation in your project
3. Use
// <template> <!-- Use default intervals --> <a-button v-copy="value">One-click copy</a-button> <!-- Custom interval time --> <!-- <a-button v-copy:2000="value">One-click copy</a-button> --> </template> <script lang="ts" setup> import {ref} from 'vue' const value = ref('This is a custom one-click copying command') </script>
Summarize
In general, this custom instruction is relatively simple. Implementation of this instruction is for the convenience of using many places in the project. This article does not explain much the code in it. If necessary, you can copy it directly into your own code to test it.
The above is the detailed content of the one-click copy function of vue custom commands. For more information about vue one-click copy, please follow my other related articles!