Angular PrimeNG TabMenu Component

08 Sep, 2021
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the TabMenu component in Angular PrimeNG.
TabMenu component: It is used to make a navigation bar that will display the nav items as a nav header ie., it is a menu in the form of tabs.
Properties:
model: It is an array of menu items. It accepts the array as an input data type & the default value is null.
activeItem: It defines the default active menu item. It accepts the menu item as an input type & the default value is null.
style: It sets the inline style of the component. It accepts the string as an input data type & the default value is null.
styleClass: It is the style class of the component. It accepts the string as an input data type & the default value is null.
Styling:
p-tabmenu: It is a container element.
p-tabmenu-nav: It is a list element of headers.
p-tabmenuitem: It is an element for the menu items.
p-menuitem-link: It is a link inside a menu item.
p-menuitem-text: It is a label of a menu item.
p-menuitem-icon: It is an icon of a menu item.
 
Creating Angular application & module installation:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save
Project Structure: After the complete installation, it will look like the following:
 
Example 1: This is the basic example that shows how to use the TabMenu component.
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG TabMenu Component</h5><p-tabMenu [model]="gfg"></p-tabMenu>
app.component.ts
import { Component } from '@angular/core';import { MenuItem } from 'primeng/api';  @Component({  selector: 'my-app',  templateUrl: './app.component.html'})export class AppComponent {  gfg: MenuItem[];    ngOnInit() {    this.gfg = [      {        label: 'HTML'      },      {        label: 'AngularJS'      },      {        label: 'ReactJS'      },      {        label: 'PrimeNG'      }    ];  }}
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 { TabMenuModule } from 'primeng/tabmenu';  @NgModule({  imports: [    BrowserModule,    BrowserAnimationsModule,    TabMenuModule,    RouterModule.forRoot([{ path: '',                     component: AppComponent }])  ],  declarations: [AppComponent],  bootstrap: [AppComponent]})export class AppModule {}
Output:
Example 2: In this example, the first item ie., HTML, in this case, will be pre-selected while loading the page for the first time.
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG TabMenu Component</h5><p-tabMenu [model]="gfg" [activeItem]="activeItem"></p-tabMenu>
app.component.ts
import { Component } from '@angular/core';import { MenuItem } from 'primeng/api';  @Component({  selector: 'my-app',  templateUrl: './app.component.html'})export class AppComponent {  gfg: MenuItem[];    activeItem: MenuItem;    ngOnInit() {    this.gfg = [      {        label: 'HTML'      },      {        label: 'AngularJS'      },      {        label: 'ReactJS'      },      {        label: 'PrimeNG'      }    ];      this.activeItem = this.gfg[0];  }}
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 { TabMenuModule } from 'primeng/tabmenu';  @NgModule({  imports: [    BrowserModule,    BrowserAnimationsModule,    TabMenuModule,    RouterModule.forRoot([{ path: '',                   component: AppComponent }])  ],  declarations: [AppComponent],  bootstrap: [AppComponent]})export class AppModule {}
Output:
Reference: https://primefaces.org/primeng/showcase/#/tabmenu
Angular-PrimeNG
AngularJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Routing in Angular 9/10
Angular PrimeNG Dropdown Component
Angular 10 (blur) Event
How to make a Bootstrap Modal Popup in Angular 9/8 ?
How to create module with Routing in Angular 9 ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?