问题是关于Angular Material的导航栏吗?

huangapple go评论69阅读模式
英文:

Probleme with angular material NavBar?

问题

这是 Header 组件的代码,其中我定义了一个菜单项数组 menuItems,其中包含标签和图标名称,这些内容将显示在导航栏中。然后在 HTML 中,我使用 Angular Material 来循环遍历列表以显示所有标签和图标。但如何添加 onclick("logout()")href: 'some route' 呢?我已经在 menuItems 表中添加了 href 属性,但在 HTML 中我无法使用 {{item.href}} 调用它,它只显示文本而不执行动作。

以下是相关代码片段:

{
  label: 'log out',
  icon: 'logout',
  href: 'click(logout())',
  showOnMobile: false,
  showOnTablet: false,
  showOnDesktop: false
},
<button
  mat-button
  *ngFor="let item of menuItems"
  [fxShow]="item.showOnDesktop"
  [fxShow.xs]="item.showOnMobile"
  [fxShow.sm]="item.showOnTablet"
>
  <mat-icon class="mr">{{item.icon}}</mat-icon>
  {{item.label}}
</button>

请注意,您在 href 属性中似乎有一个不正确的值 'click(logout())'。如果您希望在点击时执行 logout() 函数,可以考虑修改代码如下:

{
  label: 'log out',
  icon: 'logout',
  clickAction: 'logout',
  showOnMobile: false,
  showOnTablet: false,
  showOnDesktop: false
},
<button
  mat-button
  *ngFor="let item of menuItems"
  [fxShow]="item.showOnDesktop"
  [fxShow.xs]="item.showOnMobile"
  [fxShow.sm]="item.showOnTablet"
  (click)="item.clickAction === 'logout' ? logout() : navigateTo(item.clickAction)"
>
  <mat-icon class="mr">{{item.icon}}</mat-icon>
  {{item.label}}
</button>

然后,您可以在 logout() 函数中执行注销操作,并根据 clickAction 的值执行其他导航操作。请确保定义 navigateTo() 函数来处理其他导航行为。

英文:

I have defined an array in which I have the list of label and icon names that will appear in the nav bar and Then in the html I have used Angular material to loop over the list to display all labels and icons. but how can I add the onclick("logout()") or the href:'some route'? the code is bellow , I have added href property to menuItems table but I can call it in the html even with {{item.href}} only display the text not doing the action.

This is Header Component:///////

export class HeaderComponent implements OnInit{

  constructor(private userService: UserService) {
  }
  isLoggedIn: boolean = false;
  menuItems: MenuItem[] = [

    {
      label: &#39;Offers&#39;,
      icon: &#39;notes&#39;,
      href:&#39;&#39;,
      showOnMobile: false,
      showOnTablet: true,
      showOnDesktop: true
    }
    ,
    {
      label: &#39;Bookmark&#39;,
      icon: &#39;bookmark&#39;,
      href:&#39;&#39;,
      showOnMobile: false,
      showOnTablet: true,
      showOnDesktop: true
    },
    {
      label: &#39;Application&#39;,
      icon: &#39;video_stable&#39;,
      href:&#39;&#39;,
      showOnMobile: false,
      showOnTablet: false,
      showOnDesktop: true
    },
    {
      label: &#39;Claim&#39;,
      icon: &#39;send&#39;,
      href:&#39;&#39;,
      showOnMobile: false,
      showOnTablet: false,
      showOnDesktop: true
    },
    {
      label: &#39;Forum&#39;,
      icon: &#39;forum&#39;,
      href:&#39;&#39;,
      showOnMobile: true,
      showOnTablet: true,
      showOnDesktop: true
    },
    {
      label: &#39;log out&#39;,
      icon: &#39;logout&#39;,
      href:&#39;click(logout()&#39;,
      showOnMobile: false,
      showOnTablet: false,
      showOnDesktop: false
    },
    {
      label: &#39;Profile&#39;,
      icon: &#39;profile&#39;,
      href:&#39;&#39;,
      showOnMobile: false,
      showOnTablet: false,
      showOnDesktop: false
    },



  ];


  ngOnInit(): void {
  }

  logout(){
    this.userService.signout()
  }




//this is the html :::::

&lt;mat-toolbar fxLayout=&quot;row&quot; color=&quot;primary&quot;&gt;
  &lt;span fxFlex&gt;&lt;mat-icon&gt;home&lt;/mat-icon&gt; Esprit Mobility &lt;/span&gt;

  &lt;button
    mat-button
    *ngFor=&quot;let item of menuItems&quot;
    [fxShow]=&quot;item.showOnDesktop&quot;
    [fxShow.xs]=&quot;item.showOnMobile&quot;
    [fxShow.sm]=&quot;item.showOnTablet&quot;
  &gt;
    &lt;mat-icon class=&quot;mr&quot;&gt;{{item.icon}}&lt;/mat-icon&gt;
    {{item.label}}
  &lt;/button&gt;
  &lt;ng-container&gt;
    &lt;button mat-icon-button [matMenuTriggerFor]=&quot;dropMenu&quot;&gt;
      &lt;mat-icon&gt;more_vert&lt;/mat-icon&gt;
    &lt;/button&gt;
    &lt;mat-menu #dropMenu=&quot;matMenu&quot;&gt;
      &lt;ng-container *ngFor=&quot;let item of menuItems&quot;&gt;
        &lt;div
          [fxShow]=&quot;!item.showOnDesktop&quot;
          [fxShow.sm]=&quot;!item.showOnTablet&quot;
          [fxShow.xs]=&quot;!item.showOnMobile&quot;
        &gt;
          &lt;button mat-menu-item  &gt;
            &lt;mat-icon class=&quot;mr&quot;&gt;{{item.icon}}&lt;/mat-icon&gt;
            {{item.label}}
          &lt;/button&gt;
          &lt;mat-divider&gt;&lt;/mat-divider&gt;
        &lt;/div&gt;
      &lt;/ng-container&gt;
    &lt;/mat-menu&gt;
  &lt;/ng-container&gt;
&lt;/mat-toolbar&gt;

问题是关于Angular Material的导航栏吗?

Thanks in advance for any help 问题是关于Angular Material的导航栏吗?

                                             . 

答案1

得分: 1

从视图的角度来看,我会调整按钮。应该指示它触发(click)方法。if语句确保href不能为null/undefined。

<button mat-menu-item (click)="item.href ? item.href() : ''">
    <mat-icon class="mr">{{item.icon}}</mat-icon>
    {{item.label}}
</button>

从组件的角度来看,这是注销对象的外观:

{
  label: '退出',
  icon: '退出',
  href: this.logout,
  showOnMobile: false,
  showOnTablet: false,
  showOnDesktop: false,
}

看一下href的值 - 它不包含(),因为它会立即调用一个函数。

英文:

From the view perspective, I would adjust the button. It should be instructed to trigger a (click) method. The if statement ensures that the href cannot be null / undefined.

&lt;button mat-menu-item (click)=&quot;item.href ? item.href(): &#39;&#39;&quot;&gt;
            &lt;mat-icon class=&quot;mr&quot;&gt;{{item.icon}}&lt;/mat-icon&gt;
            {{item.label}}
          &lt;/button&gt;

From the component perspective, here is how the logout object looks:

{
  label: &#39;log out&#39;,
  icon: &#39;logout&#39;,
  href: this.logout,
  showOnMobile: false,
  showOnTablet: false,
  showOnDesktop: false,
},

Take a look at href value - it does not contain (), since it would immediately invoke a function

huangapple
  • 本文由 发表于 2023年7月20日 15:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定