关于vue首屏加载loading问题
注意:网上搜索出来的都是教你在index.html里面<div id="app"><div class="loading"></div>或者在app.vue Mounte生命周期函数控制app和loading的显示和隐藏,这里会有一个问题,就是js渲染页面需要时间,一样会出现几秒钟白屏。mounted(包裹使用nextTick)执行回调div app的内容依然没有开始渲染。
正确的做法是给loading一个z-index:-1,绝对定位。当app有内容时覆盖loading,确保app的内容高度至少占一屏,不然会出现覆盖不全。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><link rel="icon" href="/favicon.ico"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>demo测试</title><style>
body{width: 100vw;min-height: 100vh;position: relative;background-color: #ffffff;margin: 0;
}
#app{background-color: #ffffff;
}.loading-model {width: 100vw;height: 100vh;display: flex;align-items: center;justify-content: center;position: absolute;top: 0;z-index: -1;}.loading {width: 100px;height: 100px;border: 10px solid #176af8;border-bottom: #cccccc 10px solid;border-radius: 50%;-webkit-animation: load 1.1s infinite linear;}@-webkit-keyframes load {from {transform: rotate(0deg);}to {transform: rotate(360deg);}}</style>
</head>
<body>
<div id="app">
</div>
<div id="appLoading" class="loading-model"><div class="loading"></div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>