Vuejs2 如果图像存在,请使用Vuejs

Vuejs2 如果图像存在,请使用Vuejs,vuejs2,Vuejs2,我是vuejs新手,我有一个用于搜索图像的挂载部分: mounted () { this.UserImage = localStorage.Image this.UserName = localStorage.Name }, 当我没有图像时,this.UserImage返回字符串:data:image/jpeg;base64, 如果我没有找到图像,我可以使用什么来比较和使用v-if v-else显示一些默认图像 我的尝试是: mounted () {

我是vuejs新手,我有一个用于搜索图像的挂载部分:

mounted () {
    this.UserImage = localStorage.Image
    this.UserName  = localStorage.Name      
},
当我没有图像时,
this.UserImage
返回字符串:
data:image/jpeg;base64,

如果我没有找到图像,我可以使用什么来比较和使用v-if v-else显示一些默认图像

我的尝试是:

mounted () {
      this.$nextTick(function () {
        this.UserImage = localStorage.Image
        this.UserName  = localStorage.Name
    })
  },

您可以使用以下内容:

<img :src="userImage" v-if="userImage !== 'data:image/jpeg;base64,'">
<img src="defaultImagePath" v-else>

您可以使用以下内容:

<img :src="userImage" v-if="userImage !== 'data:image/jpeg;base64,'">
<img src="defaultImagePath" v-else>

我会这样做:

mounted () {
    this.UserImage = localStorage.Image !== 'data:image/jpeg;base64,' ? localStorage.Image : defaultPath
},
这有助于保持模板的干净

我会将不存在的图像设置为空字符串(而不是
data:image/jpeg;base64,
)。这样,您就可以使用更短的隐式if语句:

mounted () {
    this.UserImage = localStorage.Image || defaultPath
},

我会这样做:

mounted () {
    this.UserImage = localStorage.Image !== 'data:image/jpeg;base64,' ? localStorage.Image : defaultPath
},
这有助于保持模板的干净

我会将不存在的图像设置为空字符串(而不是
data:image/jpeg;base64,
)。这样,您就可以使用更短的隐式if语句:

mounted () {
    this.UserImage = localStorage.Image || defaultPath
},

你的问题不清楚。如果您知道它将是那个精确的字符串,那么使用
If
satement,与该字符串进行比较,结果为真,然后设置一个不同的图像url。您的问题不清楚。如果您知道它将是一个精确的字符串,那么使用
If
语句,与该字符串进行比较,结果为真,然后设置一个不同的图像url。