首页 技术 正文
技术 2022年11月16日
0 收藏 996 点赞 2,274 浏览 959 个字

这是从网上找到的一个案例,由于网上的案例有坑,所以我在这里从新上传一次!

首先在main.js里引入两个自定义指令

import {focus, drag} from './components/darg.js'

  然后创建一个darg.js

import Vue from 'vue'
const focus = Vue.directive('focus', {
inserted: function(el) {
el.focus()
el.setAttribute('placeholder', 'test')
}
})
const drag = Vue.directive('drag',{
inserted: function(el) { //inserted 钩子函数:当元素被插入父元素时触发,可省略
let oDiv = el; //el --> 触发的DOM元素
   oDiv.style.position='relative';
oDiv.onmousedown = function(e) {
let l = e.clientX - oDiv.offsetLeft;
let t = e.clientY - oDiv.offsetTop;
document.onmousemove = function(e) {
oDiv.style.left = e.clientX - l + 'px';
oDiv.style.top = e.clientY - t + 'px';
};
oDiv.onmouseup = function() {
document.onmousemove = null;
oDiv.onmouseup = null;
}
}
}
})export { focus, drag}

  这里面有两个自定义指令一个是自动input自动对焦,一个是div的拖拽,红色是我遇到的坑,网上代码没有{}所以我这里标出来。

最后随意创建一个.vue的文件,这里我就创建一个Hello.vue

<template>
<div>
<div class="ddd" v-drag>我可以拖拽</div>
<input type="text" v-focus />
</div>
</template>
<script>
</script>
<style>
</style>

  最后纠正下,其实顺序是先写darg.js,再引入到main.js之后就可以再Hello.vue里使用自定义指令了。、

希望对大家有帮助,谢谢

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893