avatarh.vue 1.76 KB
<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,
      twoWay: 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/default-one.png',
        realName: '默认头像',
        saveName: ''
      },
      showmsg:'',
      json:{path:'',realName:''}
    };
  },
  ready:function(){
    if(!!this.defaultjsonf){
      this.$set('defaultJson.path',this.defaultjsonf);
      this.replaceDefaultUrl();
      // this.defaultJson.path=this.defaultjsonf;
    }
  },
  
};
</script>