当前位置:网站首页>自定义指令,实现默认头像和用户上传头像的切换
自定义指令,实现默认头像和用户上传头像的切换
2022-08-09 14:54:00 【小羊卷】
整理原因:
在后台管理系统中,我们通常需要实现如果当前的用户没有设置头像,那么就显示默认头像,如果设置了头像,那么就使用用户设置的头像
因为这个功能在组件中的各个位置都有用到,所以可以使用自定义指令来实现
自定义指令的注册与使用
// 配置设置头像的自定义指令 相当于指令的第二个参数option 第一个参数是指令的名字
// 对dom进行底层操作,如果没有照片的时候就显示默认的照片
// import store from '@/store'
export const Avctar = {
// 第二个参数表示自定义指令接受的参数
// el 拿到的是当前的dom元素实例
// bing 表示绑定指令时接受的参数 bing.value
inserted: function (el, bing) {
// 监听dom元素也就是img标签的error事件,当没有照片
// 凡是需要加载网络资源的,如果当前资源加载不出来,就会触发onerror事件
// 满足条件,如果说当前没有照片的话,才进行执行
el.onerror = () => (el.src = bing.value)
},
}
使用方式
在main.js中
import { Avctar } from '@/directive'
Vue.directive('avatar', Avctar)
边栏推荐
- In the process of quantitative trading, retail investors can do this
- 常见编译问题
- Analysis: Which method is used to build a stock quantitative trading database?
- 排序方法(希尔、快速、堆)
- Qt control - QTextEdit usage record
- C#轻量级ORM使用 Dapper+Contrib
- 常见的数学物理方程
- 浅谈ArraryList的浅克隆和深克隆
- .Net Core 技巧小结
- WebGL探索——抉择:实践方向(twgl.js、Filament、Claygl、BabylonJS、ThreeJS、LayaboxJS、SceneJS、ThinkJS、ThingJS)
猜你喜欢
突然想分析下房贷利率及利息计算
Mathematica 作图详解
ASP.Net Core实战——使用Swagger
跨平台桌面应用 Electron 尝试(VS2019)
九、【Vue-Router】缓存路由组件 keep-alive标签
What is a template engine?What are the common template engines?Introduction to common commands of thymeleaf.
原子的核型结构及氢原子的波尔理论
xshell7连接工具下载
Inverted order at the beginning of the C language 】 【 string (type I like Beijing. Output Beijing. Like I)
光线的数值追踪
随机推荐
Analysis: Which method is used to build a stock quantitative trading database?
股票程序化交易如何理解自己的交易系统?
编译器不同,模式不同,对结果的影响
程序化交易规则对于整个交易系统有什么意义?
The difference between show and exec in Qt dialog
The recycle bin has been showed no problem to empty the icon
怎么用VS+Qt创建新项目
WebGL:BabylonJS入门——初探:注入活力
6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
Stock trading stylized how to understand their own trading system?
注解与反射
单向链表几个比较重要的函数(包括插入、删除、反转等)
Mathematica 数据分析(简明)
How to achieve stable profit through the stock quantitative trading interface?
CV复习:BatchNorm
How to List < Map> grouping numerical merge sort
CV复习:过拟合、欠拟合
How can I know if quantitative programmatic trading is effective?
Grad CAM model visualization
Bessel function