我是新的角4,我试图创建一个组件,显示加载框,而一些内容正在加载.像登录,加载图表等。我不想使用插件来做它,因为我想学习如何做。我使用CLI命令ng g组件创建了组件,然后创建了一个调用方法以显示组件或将其隐藏在视图中的服务。
loading.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-loading',
templateUrl: './loading.component.html',
styleUrls: ['./loading.component.scss']
})
export class LoadingComponent implements OnInit {
private loading = false;
constructor() { }
ngOnInit() {
}
public showLoad(){
this.loading = true;
}
public hideLoad(){
this.loading = false;
}
}loading.component.html
<div class ="box-loading" *ngIf="loading">
<div id="loading"></div>
</div>loading.service.ts
import { Injectable } from '@angular/core';
import { LoadingComponent } from '../../loading/loading.component';
@Injectable()
export class LoadingService {
constructor(private loading: LoadingComponent) { }
public showLoading(){
this.loading.showLoad();
}
public hideLoading(){
this.loading.hideLoad();
}
}当我调用showLoading()方法时,什么都不会发生。因此,我决定在我的登录页面上测试<app-loading></app-loading>,但是我得到了以下错误。
ERROR Error: Uncaught (in promise): Error: Template parse errors:
'app-loading' is not a known element:
1. If 'app-loading' is an Angular component, then verify that it is part of this module.
2. If 'app-loading' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<div class="login-box card">
<div class="card-body">
[ERROR ->]<app-loading *ngIf="loading"></app-loading>
<form class="form-horizontal floating-labels" id="log"): ng:///LoginModule/LoginComponent.html@4:3我将CUSTOM_ELEMENTS_SCHEMA添加到ngModule中,在loading.module.ts中,也在app.module.ts中。
loading.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoadingComponent } from './loading.component';
@NgModule({
declarations: [LoadingComponent],
exports: [LoadingComponent],
imports: [LoadingComponent],
schemas: [ CUSTOM_ELEMENTS_SCHEMA]
})
export class LoadingModule{}如何将组件插入到html页面中?有什么最佳做法吗?我在正确的道路上吗?
发布于 2018-06-13 19:39:55
一个更简单的方法是实现如下所示的加载组件:
import {Component, Input, OnInit} from '@angular/core';
@Component({
...
})
export class LoadingComponentimplements OnInit {
@Input() isLoading: boolean = false;
constructor() {
}
ngOnInit() {
}
}
在LoadingCompoenent的HTML中使用mat-spinner of Angular Material
<div class="loading-shade" *ngIf="isLoading">
<mat-spinner></mat-spinner>
</div>
在CSS中
.loading-shade {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0, 0, 0, 0.15);
z-index: 1;
display: flex;
align-items: center;
justify-content: center;
}
然后在LoadingCompoenent中注册AppModule
@NgModule({
declarations: [LoadingComponent]
})
最后,在您的应用程序中调用wyou想要的任何位置,只需将在以下组件中定义的任何@Input()变量作为loading:
在该组件中正在获取某些内容的模板的顶部,添加LoadingCompoenent的选择器
// TYPESCRIPT
loading: boolean;
doStuff(){
this.loading = true;
...
this.loading = false;
}
// HTML
<app-loading [isLoading]="loading"></app-loading>
<h1>TITLE</h1>
发布于 2018-06-13 18:28:44
您可以在服务中实现一个主题,并且组件可以如下所示:
在服役中
import { Injectable, EventEmitter } from '@angular/core';
@Injectable()
export class LoadingService {
showLoading = new EventEmitter<state>();
public showLoading(){
this.showLoading.emit(true);
}
public hideLoading(){
this.showLoading.emit(false);
}
}在component.ts文件中,订阅服务中的eventEmitter。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-loading',
templateUrl: './loading.component.html',
styleUrls: ['./loading.component.scss']
})
export class LoadingComponent implements OnInit {
private loading = false;
constructor(private loadingService: LoadingService) { }
ngOnInit() {
this.loadingService.showLoading.subscribe(
(state) => {
this.loading = state;
}
);
}
}当然,不要忘记将LoadingService添加到AppModule提供程序中。
...
@NgModule({
declarations: [LoadingComponent],
imports: [ //You don't declare your components here, only the external modules you use in your project// ],
providers: [LoadingService]
})
...通常,这些服务用于服务组件,例如从服务器获取数据,或者在组件之间进行通信以及许多其他好处,您的思维方式对于角设计模式来说不是很好的实践,我想您需要在开始编码之前熟悉角结构。
发布于 2018-06-13 19:22:49
您可以像这样加载LoadingComponent 动态:
1)在AppModule中,将LoadingComponent注册为entryComponents,如下所示:
@NgModule({
declarations: [LoadingComponent],
entryComponents: [LoadingComponent],
providers: [LoadingService]
})
2)在LoadingService中,实现如下方法的显示和隐藏:
import {
ApplicationRef, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, Injector,
} from '@angular/core';
@Injectable()
export class LoadingService {
loadingCompRef: any;
constructor(private loading: LoadingComponent,
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector,
) {}
public showLoading(){
// 1. Create a component reference from the component
this.loadingCompRef = this.componentFactoryResolver
.resolveComponentFactory(component)
.create(this.injector);
// bind data to component’s inputs
this.loadingCompRef.instance.loading= true;
// 2. Attach component to the appRef so that it's inside the ng component tree
this.appRef.attachView(this.loadingCompRef.hostView);
// 3. Get DOM element from component
const domElem = (this.loadingCompRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
// 4. Append Loding DOM element to the body
// `my-loader` is the `HTML id` attribut where you want to append you loader
document.getElementById('my-loader').appendChild(domElem);
}
public hideLoading(){
this.loadingCompRef.instance.loading= false;
this.appRef.detachView(this.loadingCompRef.hostView);
this.loadingCompRef.destroy();
}
}
有关角中Dynamic loading of components的更多细节,您可以在这里查看:https://angular.io/guide/dynamic-component-loader
https://stackoverflow.com/questions/50843843
复制相似问题