解决vue更新默认值时出现的闪屏问题
在Vue项目中,对于一个展示用户个人信息的页面。有以下需求,需要判断用户个人信息是否填充过,如果填充过,需要在页面中展示已填充项(未填充项不展示);如果未填充过,需要在页面中显示另外一种元素(提示用“去完善”个人信息)。
这时候,我们在页面中有如下元素:
<div v-if="userExist">
// 这里展示用户已填充的信息内容
</div>
<div v-else>
<img class="user-pic-tip" src="../../images/no_info.png"/>
<div class="user-info-tip">
<span>暂无基本资料数据</span>
<router-link to="/editUser">
<span class="data-link-to">去完善</span>
</router-link>
</div>
</div>
data中userExist有默认值——false:
export default {
name: "healthData",
data() {
return {
userExist: false, // 标记用户是否已填充个人信息
}
};
},
而用户到底有没有填充过个人信息是需要在mounted中通过接口从后端获取数据才可以知道,这里如果是填充过,后端会返回状态码200;如果没有填充过,后端会返回状态码201。
axios.get('/userInfo').then(res => {
if (res.data.data && res.data.status == 200) {
this.userExist = true
// 这里填充对所返回用户数据的解析
}
}
在上面这种代码情况下,因为userExist默认值为false,所以默认需要展示无数据的页面元素。又因为userExist值得变更需要通过axios请求的方式获取,就会有延时问题,即产生页面闪烁的情况。
解决方式:
这种问题我们就可以通过如下方式解决,因为无数据是页面中的元素较少,我们可以跟它们中的图片路径、文字等内容的初始值设置为空,请求结束时再为其重新赋值。
页面元素部分:
<div v-if="userExist">
// 这里展示用户已填充的信息内容
</div>
<div v-else>
<img class="user-pic-tip" :src="noDataTip.imgSrc"/>
<div class="user-info-tip">
<span>{{noDataTip.tipInfo}}</span>
<router-link to="/editUser">
<span class="data-link-to">{{noDataTip.jumpEditUser}}</span>
</router-link>
</div>
</div>
在data中进行声明:
export default {
name: "healthData",
data() {
return {
userExist: false, // 标记用户是否已填充个人信息
noDataTip: { // 用户未填写个人信息时要显示的页面元素的填充内容
imgSrc: '',
tipInfo: '',
jumpInfo: ''
}
}
};
},
重新赋值的过程:
axios.get('/userInfo').then(res => {
if (res.data.status == 10001) {
return context.noDataTip = {
imgSrc: require('../../images/no_data.png'),
tipInfo: '暂无基本资料数据',
jumpInfo: '去完善'
}
}
if (res.data.data && res.data.status == 200) {
this.userExist = true
// 这里填充对所返回用户数据的解析
}
}
通过这种方式,用户就不会看到闪烁的情况了。但是,上面这种方式有一个值得注意的地方,img标签的src属性如果想要通过属性绑定的方式给其动态赋值,有两种处理方式:1.通过上面代码中的 require('') 方式;2.通过书写绝对路径的方式。
属性绑定相关内容可以查看这篇文章:https://www.cnblogs.com/belongs-to-qinghua/p/10939900.html
解决vue更新默认值时出现的闪屏问题的更多相关文章
- PostgreSQL中实现更新默认值(二)
今天我们用表继承+触发器的方案,来实现表中的更新默认值.这也许是PostgreSQL里最佳的解决方案. 一. 创建一张表,作为父表 create table basic_update( t_updat ...
- 解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 如上图所示:频繁出现此 ...
- Lombok的使用详解(最详尽的解释,覆盖讲解所有可用注解),解决@Builder.Default默认值问题
原文:https://blog.csdn.net/f641385712/article/details/82081900 前言 Lombok是一款Java开发插件,使得Java开发者可以通过其定义的一 ...
- model中设置默认值时 ,使用 lambda 与否的差别以及datetime的默认值方法
'date': '2013-01-01' #固定值 'date': time.strftime('%Y-%m-%d') #启动时候的值 'date': lambda *a: time.strfti ...
- 解决svn更新项目目录时“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted”的报错问题
今天在IDEA更新项目目录时,发现报错“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was ...
- Vue checkbox默认值改变
<label><input v-bind:true-value="1" v-bind:false-value="0" type=" ...
- SqlServer修改某个字段的默认值时的操作步骤
sqlserver有时候需要修改一个字段的默认值,却发现修改(update)不了,也删除(delete)不了,排查发现,需要先删除原有的默认值约束,才行:步骤如下1.2.3.若原来这个字段就没有默认值 ...
- [转]解决Eclipse更新ADT插件时遇到的Eclipse reports rendering library more recent than ADT plug-in问题
使用 SDK Manager 工具更新下载新版本后,无法显示可视化布局,同时提示 This version of the rendering library is more recent than y ...
- vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.
出现原因: element-ui中 el-tab绑定的值在切换tab时会自动修改 而activeTag是从store中获取的值,不能直接修改 要添加给它绑定上set <el-tabs cla ...
随机推荐
- 【C语言】创建一个函数,并调用比较三个数的大小
#include <stdio.h> int max(int x,int y,int z) { if(x>=y) if(x>=z) return x; else return ...
- 解决:hudson.plugins.git.GitException: Could not init
解决:
- Springboot中使用kafka
注:kafka消息队列默认采用配置消息主题进行消费,一个topic中的消息只能被同一个组(groupId)的消费者中的一个消费者消费. 1.在pom.xml依赖下新添加一下kafka依赖ar包 < ...
- 「CF815C」Karen and Supermarket
传送门 Luogu 解题思路 树形背包. 设 \(f[i][j][0/1]\) 表示在以 \(i\) 为根的子树中选 \(j\) 件商品的最少花费. 边界条件: \(f[i][j][0] = \min ...
- 1143. Longest Common Subsequence
link to problem Description: Given two strings text1 and text2, return the length of their longest c ...
- Mybatis中mapper.xml的使用
详解多对多,mybatis多对多查询(xml方式和注解方式) 链接:https://blog.csdn.net/qq_42524262/article/details/98383977 链接:http ...
- 以太坊执行miner.start返回null终极解决方案
参考博文:https://blog.csdn.net/wo541075754/article/details/79260040
- POJ1797 Heavy Transportation (堆优化的Dijkstra变形)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- kafka创建topic报错
kafka执行如下创建topic的语句: [root@node01 kafka_2.11-1.0.0]# bin/kafka-topics.sh --create --topic streaming- ...
- mcast_join_source_group函数
#include <errno.h> #include <net/if.h> #include <sys/socket.h> #define SA struct s ...