英文:
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: 'Offers',
icon: 'notes',
href:'',
showOnMobile: false,
showOnTablet: true,
showOnDesktop: true
}
,
{
label: 'Bookmark',
icon: 'bookmark',
href:'',
showOnMobile: false,
showOnTablet: true,
showOnDesktop: true
},
{
label: 'Application',
icon: 'video_stable',
href:'',
showOnMobile: false,
showOnTablet: false,
showOnDesktop: true
},
{
label: 'Claim',
icon: 'send',
href:'',
showOnMobile: false,
showOnTablet: false,
showOnDesktop: true
},
{
label: 'Forum',
icon: 'forum',
href:'',
showOnMobile: true,
showOnTablet: true,
showOnDesktop: true
},
{
label: 'log out',
icon: 'logout',
href:'click(logout()',
showOnMobile: false,
showOnTablet: false,
showOnDesktop: false
},
{
label: 'Profile',
icon: 'profile',
href:'',
showOnMobile: false,
showOnTablet: false,
showOnDesktop: false
},
];
ngOnInit(): void {
}
logout(){
this.userService.signout()
}
//this is the html :::::
<mat-toolbar fxLayout="row" color="primary">
<span fxFlex><mat-icon>home</mat-icon> Esprit Mobility </span>
<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>
<ng-container>
<button mat-icon-button [matMenuTriggerFor]="dropMenu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #dropMenu="matMenu">
<ng-container *ngFor="let item of menuItems">
<div
[fxShow]="!item.showOnDesktop"
[fxShow.sm]="!item.showOnTablet"
[fxShow.xs]="!item.showOnMobile"
>
<button mat-menu-item >
<mat-icon class="mr">{{item.icon}}</mat-icon>
{{item.label}}
</button>
<mat-divider></mat-divider>
</div>
</ng-container>
</mat-menu>
</ng-container>
</mat-toolbar>
Thanks in advance for any help
.
答案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.
<button mat-menu-item (click)="item.href ? item.href(): ''">
<mat-icon class="mr">{{item.icon}}</mat-icon>
{{item.label}}
</button>
From the component perspective, here is how the logout object looks:
{
label: 'log out',
icon: 'logout',
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论