Angular PrimeNG TabView 懒散加载
Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,这个框架可用于制作响应式网站,非常容易。在这篇文章中,我们将看到如何在Angular PrimeNG中使用TabView Lazy Loading。
Angular PrimeNG为TabView组件提供了便利,该组件作为一个容器,以便将内容与标签分组。懒惰加载(Lazy Loading)用于懒惰加载TabView数据。懒惰加载通过只初始化活动标签来帮助初始加载性能,非活动标签在被选中之前不会被初始化。当有一个ngTemplate的pTemplate=”content “时,TabPanel被指定为懒惰。
语法:
<p-tabPanel header="...">
<ng-template pTemplate="content">
<p>...</p>
</ng-template>
</p-tabPanel>
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:它将看起来像如下。
例子1:下面是一个简单的例子,演示Angular PrimeNG TabView Lazy Loading的使用。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Lazy Loading</h4>
<p-tabView>
<p-tabPanel header="Coding">
<p>Code Here</p>
</p-tabPanel>
<p-tabPanel header="Web Technologies">
<ng-template pTemplate="content">
<p>Learn About Web Technologies</p>
</ng-template>
</p-tabPanel>
<p-tabPanel header="Articles">
<ng-template pTemplate="content">
<p>Read some Tech Articles</p>
</ng-template>
</p-tabPanel>
</p-tabView>
- app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [],
})
export class AppComponent {
constructor() { }
ngOnInit() { }
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TabViewModule,
ButtonModule,
RouterModule.forRoot([
{ path: '', component: AppComponent }
])
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出:
实例2:下面是另一个演示Angular PrimeNG TabView懒惰加载的例子,我们在动态标签上使用懒惰加载。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Lazy Loading</h4>
<p-tabView [(activeIndex)]="index">
<p-tabPanel [header]="item.header"
*ngFor="let item of items;
let i = index"
[selected]="i == 0">
<ng-template pTemplate="content">
{{ item.content }}
</ng-template>
</p-tabPanel>
</p-tabView>
- app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [],
})
export class AppComponent {
constructor() { }
ngOnInit() { }
items = [
{ header: 'Coding', content: 'Lets Code' },
{
header: 'Web Technologies',
content: 'Lets Learn About Web Technologies',
},
{
header: 'Articles',
content: 'Lets Read some Tech Articles' },
];
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TabViewModule,
ButtonModule,
RouterModule.forRoot([
{ path: '', component: AppComponent }
])
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出: