我創造了4角的應用程序,我想有一個加載組件,使用戶能夠意識到他提交表單和應用程序在做什麼,並且該應用程序正在等待來自後端的信息。加載組件/ 4
我這樣做是在angularjs通過具有Loader組件和共享的行動,隱藏或顯示使用$ rootScope ......但在angular2/4,我不知道怎樣才能做到這一點。
理想情況下,我需要有一個加載組件,這將是在一個窗體或頁面的某些部分(當屬於該部分的信息被檢索),或者可能在整個屏幕上。
您能給一個關於如何做到這一點的線索?
謝謝!
我創造了4角的應用程序,我想有一個加載組件,使用戶能夠意識到他提交表單和應用程序在做什麼,並且該應用程序正在等待來自後端的信息。加載組件/ 4
我這樣做是在angularjs通過具有Loader組件和共享的行動,隱藏或顯示使用$ rootScope ......但在angular2/4,我不知道怎樣才能做到這一點。
理想情況下,我需要有一個加載組件,這將是在一個窗體或頁面的某些部分(當屬於該部分的信息被檢索),或者可能在整個屏幕上。
您能給一個關於如何做到這一點的線索?
謝謝!
您將要創建一個可以存儲到您的裝載組件的引用一個加載的服務。然後將該加載服務注入到需要能夠切換該加載組件的其他組件的構造器中。
import { Injectable } from '@angular/core';
import { LoadingComponent } from './loading.component';
@Injectable()
export class LoadingService {
private instances: {[key: string]: LoadingComponent} = {};
public registerInstance(name: string, instance: LoadingComponent) {
this.instances[name] = instance;
}
public removeInstance(name: string, instance: LoadingComponent) {
if (this.instances[name] === instance) {
delete this.instances[name];
}
}
public hide(name: string) {
this.instances[name].hide();
}
public show(name: string) {
this.instances[name].show();
}
}
一定要在你的模塊providers
陣列註冊您的LoadingService
!
然後在LoadingComponent
,你可以注入LoadingService
,使LoadingComponent
本身可以與LoadingService
註冊它應與服務本身註冊:
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { LoadingService } from './loading.service';
@Component({
selector: 'yourapp-loading',
templateUrl: './loading.component.html',
styleUrls: ['./loading.component.scss']
})
export class LoadingComponent implements OnInit, OnDestroy {
@Input() name: string;
private isVisible = false;
constructor(private service: LoadingService) {}
ngOnInit() {
if (this.name) {
this.service.registerInstance(this.name, this);
}
}
ngOnDestroy() {
if (this.name) {
this.service.removeInstance(this.name, this);
}
}
/* ... code to show/hide this component */
}
基本上你可以有一個loader
組件與loader
作爲HTML,您可以從主設備component
設置值。像下面這樣,我用material2
爲例。
你spinner.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-loader',
templateUrl: './loader.component.html',
styleUrls: ['./loader.component.css']
})
export class LoaderComponent{
@Input() show:boolean;
}
HTML:
<div *ngIf="show">
<md-spinner></md-spinner>
</div>
組件,你想用你的spinner
。
<app-loader [show]="showLoader">
</app-loader>
而在你的TS只是showLoader
值設置爲真/假。
P.S - 確保你照顧它的position
和z-index
,這樣它就成爲最重要的一切。
是的,但在這個解決方案中,我需要在每個組件中添加應用程序加載器,我希望有一個加載器...我正在尋找一些方法,我可以在app.component或類似的東西中添加一次我可以在沒有太多努力的情況下在每個組件中重複使用 – Faabass
這可以幫助你 像輸入類成員
import { Component, OnInit,Input } from '@angular/core';
@Component({
selector: 'app-loader',
templateUrl: './loader.component.html',
styleUrls: ['./loader.component.css']
})
export class LoaderComponent implements OnInit {
@Input() loading: boolean = false;
constructor() { }
ngOnInit() {
}
}
,並在模板
<div class="ui " *ngIf="loading">
<div class="ui active dimmer">
<div class="ui large text loader">Loading</div>
</div>
<p></p>
</div>
下在上面的代碼創建一個組件調用加載組件我正在使用semantic-ui框架的loader。你可以使用任何基於你的項目的loader要求。接下來的成分,你必須使用的裝載機,模板只使用加載器組件作爲子組件這樣
<div class="center">
<app-loader [loading]="loading"></app-loader>
</div>
,並在.TS文件中聲明輸入varaiable爲
loading: boolean = false;
當你啓動裝載器,才使
this.loading=true
,併爲您的裝載機將up.To解僱裝載只是讓
this.loading=false;
這應該做的trick.Its經過精美的here
感謝您的回答!是的,儘管如此...但我的想法是不要在每個組件中使用
取決於你的裝載器是否可以通過編程方式啓動而不使用html。你正在使用哪個裝載器? –
看來這解釋了什麼是我要找的......你能請詳細說明一下「可以存儲對你的加載組件的引用的服務」的部分。我不清楚如何做到這一點 – Faabass
這項工作就像一個魅力!謝謝! – Faabass