最簡單的方法是在應用程序準備好時將CSS動畫放入應用程序引導程序組件中,以替換內容。
請注意,由於腳本縮小以及更改檢測將只運行一次,因此應用程序將以不同的速度加載,具體取決於它是開發版本還是生產版本,因此可縮短加載時間。 您可能需要調整動畫的速度。 來自應用程序的Http請求也可能發揮作用。
我得到這個來自:根組件http://www.alessioatzeni.com/blog/css3-loading-animation/
CSS和HTML:
<app-root>
<style>
#content {
width: 100%;
/* Full Width */
height: 1px;
margin: 1px auto;
background: #000;
}
.expand {
width: 100%;
height: 1px;
margin: 1px 0;
background: #2187e7;
position: absolute;
box-shadow: 0px 0px 10px 1px rgba(0, 198, 255, 0.7);
/*adjust speed here */
-moz-animation: fullexpand 1.5s ease-out;
-webkit-animation: fullexpand 1.5s ease-out;
}
/* Full Width Animation Bar */
@-moz-keyframes fullexpand {
0% {
width: 0px;
}
100% {
width: 100%;
}
}
@-webkit-keyframes fullexpand {
0% {
width: 0px;
}
100% {
width: 100%;
}
;
}
</style>
<div id="content">
<span class="expand">Loading...</span>
</div>
</app-root>
第二個選項是訪問bootstrap
功能槽system.js
,使之成爲index.html
可供script標籤。
這是一篇由Ben Nadel撰寫的博客文章如何做到這一點。
演示:Creating A Pre-Bootstrap Loading Screen In Angular 2 RC 1
(function(global) {
// .... code removed ....
System.config({
// .... code removed ....
});
// Load "./app/main.ts" (gets full path from package configuration above).
// --
// NOTE: We are attaching the resultant promise to the global scope so that other
// scripts may listen for the successful loading of the application.
global.bootstrapping = System
.import("app")
.then(
function handleResolve() {
console.info("System.js successfully bootstrapped app.");
},
function handleReject(error) {
console.warn("System.js could not bootstrap the app.");
console.error(error);
return(Promise.reject(error));
}
)
;
})(window);
Ben Nadel在他的博客中發佈了一個更新的解決方案。看看這裏 - https://www.bennadel.com/blog/3147-creating-an-event-driven-pre-bootstrap-loading-screen-in-angular-2-0-0.htm。但是,我仍然不明白DOM事件和JavaScript代碼堆積如山,這就是爲什麼我要使用這裏發佈的簡單解決方案 - http://stackoverflow.com/a/35244932/968003。 –