Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

vue条件渲染问题

### 问题描述
页面加载时,会先出现v-else下的结果,然后才出现v-if下的结果
### 相关代码

<el-image class="miner-img" :src="'https://cloud.dxpool.com/' + minerInfo.img_url" fit="contain" v-if="minerInfo.img_url"></el-image>
<el-image class="miner-img" :src="require('../../../assets/images/noimage.svg')" v-else></el-image>

### 你期待的结果是什么?实际看到的错误信息又是什么?
希望在v-if="minerInfo.img_url"为真时,直接输出图片,不经过v-else


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这个你刚开始请求数据的时候,img_url 字段是没有的, 所以会走 else 逻辑。

你可以加个 loadingFlag 状态,默认是true, 当加载完数据的时候 设置为 false, 大致代码如下:

<el-image class="miner-img" :src="'https://cloud.dxpool.com/' + minerInfo.img_url" fit="contain" v-if="minerInfo.img_url"></el-image>
<el-image class="miner-img" :src="require('../../../assets/images/noimage.svg')" v-else-if="!loadingFlag"></el-image>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...