安装 VueUse npm i @vueuse/core
pack.json中显示版本
"@vue/composition-api": "^1.0.0", // (不知道啥原因,必须下载这个才可以使 用,否则报错) npm 下载一下:npm install @vue/composition-api
"@vueuse/core": "^5.1.3",
js中引入方法
import { onClickOutside } from '@vueuse/core'
使用
HTML中:
<div ref="target" ></div>
JS中:
methods:{
cickOutside() {
onClickOutside(this.$refs.target, (e) => {
this.selectShow = false;
});
}
}
mounted () {
this.cickOutside() //此方法,放在mounted中,放在created函数中的话,会出现页面dom还没渲染成功,从而无法获取ref,导致方法无效。
}