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

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

Probleme with angular material NavBar?

问题

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

以下是相关代码片段:

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

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

  1. {
  2. label: 'log out',
  3. icon: 'logout',
  4. clickAction: 'logout',
  5. showOnMobile: false,
  6. showOnTablet: false,
  7. showOnDesktop: false
  8. },
  1. <button
  2. mat-button
  3. *ngFor="let item of menuItems"
  4. [fxShow]="item.showOnDesktop"
  5. [fxShow.xs]="item.showOnMobile"
  6. [fxShow.sm]="item.showOnTablet"
  7. (click)="item.clickAction === 'logout' ? logout() : navigateTo(item.clickAction)"
  8. >
  9. <mat-icon class="mr">{{item.icon}}</mat-icon>
  10. {{item.label}}
  11. </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.

  1. This is Header Component:///////
  2. export class HeaderComponent implements OnInit{
  3. constructor(private userService: UserService) {
  4. }
  5. isLoggedIn: boolean = false;
  6. menuItems: MenuItem[] = [
  7. {
  8. label: &#39;Offers&#39;,
  9. icon: &#39;notes&#39;,
  10. href:&#39;&#39;,
  11. showOnMobile: false,
  12. showOnTablet: true,
  13. showOnDesktop: true
  14. }
  15. ,
  16. {
  17. label: &#39;Bookmark&#39;,
  18. icon: &#39;bookmark&#39;,
  19. href:&#39;&#39;,
  20. showOnMobile: false,
  21. showOnTablet: true,
  22. showOnDesktop: true
  23. },
  24. {
  25. label: &#39;Application&#39;,
  26. icon: &#39;video_stable&#39;,
  27. href:&#39;&#39;,
  28. showOnMobile: false,
  29. showOnTablet: false,
  30. showOnDesktop: true
  31. },
  32. {
  33. label: &#39;Claim&#39;,
  34. icon: &#39;send&#39;,
  35. href:&#39;&#39;,
  36. showOnMobile: false,
  37. showOnTablet: false,
  38. showOnDesktop: true
  39. },
  40. {
  41. label: &#39;Forum&#39;,
  42. icon: &#39;forum&#39;,
  43. href:&#39;&#39;,
  44. showOnMobile: true,
  45. showOnTablet: true,
  46. showOnDesktop: true
  47. },
  48. {
  49. label: &#39;log out&#39;,
  50. icon: &#39;logout&#39;,
  51. href:&#39;click(logout()&#39;,
  52. showOnMobile: false,
  53. showOnTablet: false,
  54. showOnDesktop: false
  55. },
  56. {
  57. label: &#39;Profile&#39;,
  58. icon: &#39;profile&#39;,
  59. href:&#39;&#39;,
  60. showOnMobile: false,
  61. showOnTablet: false,
  62. showOnDesktop: false
  63. },
  64. ];
  65. ngOnInit(): void {
  66. }
  67. logout(){
  68. this.userService.signout()
  69. }
  70. //this is the html :::::
  71. &lt;mat-toolbar fxLayout=&quot;row&quot; color=&quot;primary&quot;&gt;
  72. &lt;span fxFlex&gt;&lt;mat-icon&gt;home&lt;/mat-icon&gt; Esprit Mobility &lt;/span&gt;
  73. &lt;button
  74. mat-button
  75. *ngFor=&quot;let item of menuItems&quot;
  76. [fxShow]=&quot;item.showOnDesktop&quot;
  77. [fxShow.xs]=&quot;item.showOnMobile&quot;
  78. [fxShow.sm]=&quot;item.showOnTablet&quot;
  79. &gt;
  80. &lt;mat-icon class=&quot;mr&quot;&gt;{{item.icon}}&lt;/mat-icon&gt;
  81. {{item.label}}
  82. &lt;/button&gt;
  83. &lt;ng-container&gt;
  84. &lt;button mat-icon-button [matMenuTriggerFor]=&quot;dropMenu&quot;&gt;
  85. &lt;mat-icon&gt;more_vert&lt;/mat-icon&gt;
  86. &lt;/button&gt;
  87. &lt;mat-menu #dropMenu=&quot;matMenu&quot;&gt;
  88. &lt;ng-container *ngFor=&quot;let item of menuItems&quot;&gt;
  89. &lt;div
  90. [fxShow]=&quot;!item.showOnDesktop&quot;
  91. [fxShow.sm]=&quot;!item.showOnTablet&quot;
  92. [fxShow.xs]=&quot;!item.showOnMobile&quot;
  93. &gt;
  94. &lt;button mat-menu-item &gt;
  95. &lt;mat-icon class=&quot;mr&quot;&gt;{{item.icon}}&lt;/mat-icon&gt;
  96. {{item.label}}
  97. &lt;/button&gt;
  98. &lt;mat-divider&gt;&lt;/mat-divider&gt;
  99. &lt;/div&gt;
  100. &lt;/ng-container&gt;
  101. &lt;/mat-menu&gt;
  102. &lt;/ng-container&gt;
  103. &lt;/mat-toolbar&gt;

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

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

  1. .

答案1

得分: 1

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

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

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

  1. {
  2. label: '退出',
  3. icon: '退出',
  4. href: this.logout,
  5. showOnMobile: false,
  6. showOnTablet: false,
  7. showOnDesktop: false,
  8. }

看一下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.

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

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

  1. {
  2. label: &#39;log out&#39;,
  3. icon: &#39;logout&#39;,
  4. href: this.logout,
  5. showOnMobile: false,
  6. showOnTablet: false,
  7. showOnDesktop: false,
  8. },

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:

确定