Angular PrimeNG MegaMenu组件

Angular PrimeNG MegaMenu组件

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,这些组件被用来做很好的造型,这个框架被用来做响应式网站,非常容易。在这篇文章中,我们将了解如何在Angular PrimeNG中使用MegaMenu组件。我们还将了解到属性、样式以及代码中使用的语法。

MegaMenu组件:它是一个导航组件,用来做一个有多个数字的菜单的组件。

属性:

  • model。它是一个菜单项的数组。它接受数组数据类型作为输入,默认值为空。
  • 方向。它用于定义方向。它是字符串数据类型,默认值为水平。
  • style。它用于设置组件的内联风格。它的数据类型为字符串,默认值为空。
  • styleClass。它用于设置组件的风格类别。它的数据类型为字符串,默认值为空。
  • baseZIndex。它用于设置分层时使用的基础ZIndex值。它接受数字数据类型作为输入,默认值为0。
  • autoZIndex。它用于指定是否自动管理分层。它是布尔数据类型,默认值为true。

样式:

  • p-megamenu。它是一个容器元素。
  • p-menu-list。它是一个列表元素。
  • p-menuitem。它是一个menuitem元素。
  • p-menuitem-text。它是一个菜单项的标签。
  • p-menuitem-icon。它是一个菜单项的图标。
  • p-submenu-icon。它是一个子菜单的箭头图标。

创建Angular应用程序和模块安装。

  • 第1步:使用以下命令创建一个Angular应用程序。
ng new appname
  • 第2步:在创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
  • 第3步:在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save

项目结构。它将看起来像以下。

Angular PrimeNG MegaMenu组件

例子1:这是一个基本的例子,说明了如何使用MegaMenu组件。

<h2>GeeksforGeeks</h2>
<h5>PrimeNG MegaMenu Component</h5>
<p-megaMenu [model]="gfg"></p-megaMenu>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
  
import { AppComponent } from './app.component';
import { MegaMenuModule } from 'primeng/megamenu';
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            MegaMenuModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { MegaMenuItem } from 'primeng/api';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent {
  gfg: MegaMenuItem[];
  
  ngOnInit() {
    this.gfg = [
      {
        label: 'GeeksforGeeks',
        items: [
          [
            {
              label: 'AngularJS',
              items: [{ label: 'AngularJS 1' }, { label: 'AngularJS 2' }]
            },
            {
              label: 'ReactJS',
              items: [{ label: 'ReactJS 1' }, { label: 'ReactJS 2' }]
            }
          ],
          [
            {
              label: 'HTML',
              items: [{ label: 'HTML 1' }, { label: 'HTML 2' }]
            },
            {
              label: 'PrimeNG',
              items: [{ label: 'PriemNG 1' }, { label: 'PrimeNG 2' }]
            }
          ]
        ]
      }
    ];
  }
}

输出:

Angular PrimeNG MegaMenu组件

示例2:在这个例子中,我们将知道如何在MegaMenu组件中使用orientation属性。

<h2>GeeksforGeeks</h2>
<h5>PrimeNG MegaMenu Component</h5>
<p-megaMenu [model]="gfg" orientation='vertical'></p-megaMenu>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
  
import { AppComponent } from './app.component';
import { MegaMenuModule } from 'primeng/megamenu';
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
            MegaMenuModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { MegaMenuItem } from 'primeng/api';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent {
  gfg: MegaMenuItem[];
  
  ngOnInit() {
    this.gfg = [
      {
        label: 'GeeksforGeeks',
        items: [
          [
            {
              label: 'AngularJS',
              items: [{ label: 'AngularJS 1' }, { label: 'AngularJS 2' }]
            },
            {
              label: 'ReactJS',
              items: [{ label: 'ReactJS 1' }, { label: 'ReactJS 2' }]
            }
          ],
          [
            {
              label: 'HTML',
              items: [{ label: 'HTML 1' }, { label: 'HTML 2' }]
            },
            {
              label: 'PrimeNG',
              items: [{ label: 'PriemNG 1' }, { label: 'PrimeNG 2' }]
            }
          ]
        ]
      }
    ];
  }
}

输出:

Angular PrimeNG MegaMenu组件

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程