直接上代码

1
2
3
export default {
avatar: require("@/assets/images/default/avatar.png")
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Vue from "vue";
import parameters from "parameters";

Vue.directive("default-img", {
bind: function(el, binding) {
el.onerror = function() {
let arg = parameters[binding.arg];
if (arg) {
el.onerror = null;
el.src = arg;
}
};
}
});

使用方式

<img v-default-img:avatar :src="Avatar" />