avatarSupplier.vue
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<template>
<img :src="json.path" :alt="json.realName"
class="avatar" @error="replaceDefaultUrl" :style='style'>
<!-- {{showmsg}}@error="replaceDefaultUrl" -->
</template>
<script>
module.exports = {
props: {
jsonStr: {
type: String,
require: true
},
style:String,
defaultjsonf:{
type:String,
require: true
}
},
/* computed: {
json: function () {
if(this.jsonStr!=null){
var imgArray = JSON.parse(this.jsonStr);
var filepath = imgArray[0].thumbnail.path + imgArray[0].thumbnail.name;
var imgJson = {path:filepath,realName:'头像'};
return imgJson;
}
return this.defaultJson;
}
},*/
watch:{
'jsonStr':function(){
if(this.jsonStr!=null){
var imgArray = JSON.parse(this.jsonStr), imgReg = /\.(jpg|jpeg|png|gif|bmp)$/i; //判断字符串是否为图片路径;
var filepath = imgReg.test(imgArray[0].thumbnail.path)? imgArray[0].thumbnail.path : imgArray[0].thumbnail.path + imgArray[0].thumbnail.name;
var imgJson = {path:filepath,realName:'头像'};
this.$set('json',imgJson)
}
}
},
methods: {
replaceDefaultUrl: function () {
this.json = {
path: this.defaultJson.path,
realName: this.defaultJson.realName
};
}
},
data: function () {
return {
defaultJson: {
path: '/images/user-default.png',
realName: '默认头像',
saveName: ''
},
showmsg:'',
json:{path:'',realName:''}
};
},
ready:function(){
if(!!this.defaultjsonf){
this.defaultJson.path=this.defaultjsonf;
}
}
};
</script>