简体   繁体   English

Angular - 在多个组件中包含 html 的最佳方法是什么?

[英]Angular - What's the best way to include html in multiple components?

I need to put a loading in multiple components of my project.我需要在我的项目的多个组件中加载。 So instead of putting the same HTML over and over again across the components, I need to know what's the best way to not repeat code, but I don't know if this is correct thing to do.因此,与其在组件中一遍又一遍地放置相同的 HTML,我需要知道不重复代码的最佳方法是什么,但我不知道这样做是否正确。 I have created a component called loading-graphic , which I bind it in every HTML file of the respective components.我创建了一个名为loading-graphic的组件,我将它绑定到各个组件的每个 HTML 文件中。 I read about ngTemplateOutlet and ngContent , but to be honest it doesn't make sense in my head to use it for this case (and I don't get it too... I'm a beginner on it).我阅读了ngTemplateOutletngContent ,但老实说,在我的脑海中将它用于这种情况是没有意义的(而且我也不明白......我是它的初学者)。 So, on what should I bet?那么,我应该押注什么? Thanks.谢谢。

Base on your question, I think creating Reusable Components with NgTemplateOutlet would be the best solution to avoid repeating HTML in different component Templates.根据您的问题,我认为使用 NgTemplateOutlet 创建可重用组件将是避免在不同组件模板中重复 HTML 的最佳解决方案。 It allows you to pass parameters base on your host component and makes your Angular app easier to test and develop since it sllows easily modified reusable component for various use cases without having to modify individual components itself.它允许您根据主机组件传递参数,并使您的 Angular 应用程序更易于测试和开发,因为它可以为各种用例轻松修改可重用组件,而无需修改单个组件本身。

Since you are a begginer I am going to Illustrate simple way of using NgTemplateOutlet, however dive deep later on Templates and Stamps.由于您是初学者,我将说明使用 NgTemplateOutlet 的简单方法,但稍后会深入研究模板和图章。

Imaging you have a reusable Search component where you want to hide a check box base on the parent component.想象一下,您有一个可重用的 Search 组件,您想在其中隐藏基于父组件的复选框。 Your Template will look like below.您的模板将如下所示。

we pass data from the parent component to the child/Search component using @Input and property binding, so we define which checkboxes to hide base on Parent component.我们使用@Input 和属性绑定将数据从父组件传递到子/搜索组件,因此我们定义了基于父组件隐藏哪些复选框。

here is the code sample for Search Component这是搜索组件的代码示例

 search.component.ts ====================== import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core'; @Component({ selector: 'app-search', templateUrl: './app-search.component.html', styleUrls: ['./app-search.component.css'] }) export class AppSearchComponent implements OnInit { accountName: string = ''; @Output() accountSearchChange = new EventEmitter<string>(); //1. Event Binding to pass data from Child to Parent Component ->Int @Input() searchMode: 'account' | 'holder' | 'distribution' = 'account'; //Use NgTemplateOutlet for reusable componenet constructor() { } ngOnInit() { } //2. Event Binding to pass data from Child to Parent Component ->Invoke Emit doSearchFilter(searchText: string) { console.log('Search Child: doSearchFilter -> ' + searchText); this.accountSearchChange.emit(searchText); } clearFilters() { console.log('Account Search: Clear Filter is called'); this.accountName = ''; } }
 search.component.html ===================== <ng-container [ngSwitch]="searchMode"> <div class="input-full-width" *ngSwitchCase="'account'"> <mat-checkbox class="example-container check-full-width">Show active and pending only</mat-checkbox> </div> <div class="input-full-width" *ngSwitchCase="'holder'"> <mat-checkbox class="example-container check-full-width">View only holders with missing requirements</mat-checkbox> </div> <div class="input-full-width" *ngSwitchCase="'holder'"> <mat-checkbox class="example-container check-full-width">View only active Holders</mat-checkbox> </div> </ng-container>

I am using Search component inside Account component and below is the code sample.我在 Account 组件中使用 Search 组件,下面是代码示例。 in HTML file i am referring app-search css selector and pass the search Mode defined in ts.在 HTML 文件中,我指的是 app-search css 选择器并传递 ts.css 中定义的搜索模式。

 import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'; import { MatSort, MatTableDataSource, MatPaginator } from '@angular/material'; import { Router } from "@angular/router"; import { Observable } from 'rxjs'; import { AccountService } from 'src/app/core/services/account.service'; import { Deal } from 'src/app/core/models/deal'; @Component({ selector: 'app-account', templateUrl: './account.component.html', styleUrls: ['./account.component.css'] }) export class AccountsComponent implements OnInit, AfterViewInit { displayedColumns: string[] = ['accountId', 'accountShortName', 'accountType']; public dealDataSource = new MatTableDataSource<Deal>(); dealsObservable: Observable<Deal[]>; searchMode = 'distribution'; isLoadingResults = true; @ViewChild(MatSort) sort: MatSort; @ViewChild(MatPaginator) paginator: MatPaginator; constructor(private router: Router, private api: AccountService) { } ......................................................
 <app-search (accountSearchChange)='doFilter($event)' [searchMode]="searchMode"></app-search>

Hope this is clear.希望这很清楚。

i think loading component is not a bad Idee.我认为加载组件不是一个糟糕的想法。 Use loading component in your app-root component.在您的 app-root 组件中使用加载组件。 You can use a loading.service and interceptor to display or hide the component.您可以使用 loading.service 和拦截器来显示或隐藏组件。 You will get automatically a Loading Indicator for each API call.您将自动获得每个 API 调用的加载指示器。

sample: https://medium.com/@zeljkoradic/loader-bar-on-every-http-request-in-angular-6-60d8572a21a9示例: https : //medium.com/@zeljkoradic/loader-bar-on-every-http-request-in-angular-6-60d8572a21a9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM