我将Angular 12与ESLint一起使用,ESLint打印出以下错误:
70:3错误使用@HostBinding或@HostListener而不是主机
元数据属性(https://angular.io/styleguide#style-06-03)@angular eslint/no主机元数据属性
有很多类似的主题,比如这个,但我的主机更复杂。
host: {
'[class.active-menuitem]':
'(selectedKey && app.isHorizontal()) || (active && !app.isHorizontal()) ' +
'|| (active && !root && app.isHorizontal())',
'[class.active-rootmenuitem]': 'active && root && app.isHorizontal()'
},
我应该做什么?比如:
@HostBinding('[class.active-menuitem]') menuItem = '(selectedKey && app.isHorizontal()) || (active && !app.isHorizontal()) ' + '|| (active && !root && app.isHorizontal())';
?
import { Component, Input, OnInit, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { LayoutComponent } from '../layout.component';
import { MenuService } from '@core/services';
@Component({
selector: '[app-menu-item]',
template: `
<ng-container>
<a
[attr.href]="item.url"
(click)="itemClick($event)"
*ngIf="!item.routerLink || item.items"
pRipple
[ngClass]="item.class"
(mouseenter)="onMouseEnter()"
(keydown.enter)="itemClick($event)"
[attr.target]="item.target"
[attr.tabindex]="0"
>
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
<span class="layout-menuitem-text">{{ item.label }}</span>
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
</a>
<a
(click)="itemClick($event)"
(mouseenter)="onMouseEnter()"
*ngIf="item.routerLink && !item.items"
pRipple
[ngClass]="item.class"
[routerLink]="item.routerLink"
routerLinkActive="active-menuitem-routerlink"
[routerLinkActiveOptions]="{ exact: true }"
[attr.target]="item.target"
[attr.tabindex]="0"
>
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
<span class="layout-menuitem-text">{{ item.label }}</span>
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
</a>
<ul
*ngIf="item.items && (active || animating || selectedKey)"
(@children.done)="onAnimationDone()"
[ngStyle]="{ padding: active && root ? '' : '0' }"
[@children]="
app.isHorizontal() && root && !app.isMobile()
? active
? 'visible'
: 'hidden'
: active
? 'visibleAnimated'
: 'hiddenAnimated'
"
>
<ng-template ngFor let-child let-i="index" [ngForOf]="item.items">
<li
app-menu-item
[item]="child"
[index]="i"
[parentKey]="key"
[class]="child.badgeClass"
></li>
</ng-template>
</ul>
</ng-container>
`,
host: {
'[class.active-menuitem]':
'(selectedKey && app.isHorizontal()) || (active && !app.isHorizontal()) ' +
'|| (active && !root && app.isHorizontal())',
'[class.active-rootmenuitem]': 'active && root && app.isHorizontal()'
},
animations: [
trigger('children', [
state(
'void',
style({
height: '0px'
})
),
state(
'hiddenAnimated',
style({
height: '0px'
})
),
state(
'visibleAnimated',
style({
height: '*'
})
),
state(
'visible',
style({
height: '*',
'z-index': 999
})
),
state(
'hidden',
style({
height: '0px',
'z-index': '*'
})
),
transition(
'visibleAnimated => hiddenAnimated',
animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')
),
transition(
'hiddenAnimated => visibleAnimated',
animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')
),
transition(
'void => visibleAnimated, visibleAnimated => void',
animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')
)
])
]
})
export class MenuItemComponent implements OnInit, OnDestroy {
@Input() item: any;
@Input() index: number;
@Input() root: boolean;
@Input() parentKey: string;
animating: boolean;
active = false;
menuSourceSubscription: Subscription;
menuResetSubscription: Subscription;
key: string;
selectedKey: boolean;
constructor(
public app: LayoutComponent,
public router: Router,
private cd: ChangeDetectorRef,
private menuService: MenuService
) {
this.menuSourceSubscription = this.menuService.menuSource$.subscribe((key) => {
// deactivate current active menu
if (this.active && this.key !== key && key.indexOf(this.key) !== 0) {
this.active = false;
}
});
this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
this.active = false;
});
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((params) => {
if (this.app.isHorizontal() && this.item.routerLink) {
this.active = false;
this.selectedKey = this.router.isActive(
this.item.routerLink[0],
this.item.items ? false : true
);
} else {
if (this.item.routerLink) {
this.updateActiveStateFromRoute();
} else {
this.active = false;
}
}
});
}
ngOnInit() {
if (!this.app.isHorizontal() && this.item.routerLink) {
this.updateActiveStateFromRoute();
}
if (this.app.isHorizontal() && this.item.routerLink) {
this.active = false;
this.selectedKey = this.router.isActive(
this.item.routerLink[0],
this.item.items ? false : true
);
}
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
}
updateActiveStateFromRoute() {
this.active = this.router.isActive(this.item.routerLink[0], this.item.items ? false : true);
}
itemClick(event: Event) {
// avoid processing disabled items
if (this.item.disabled) {
event.preventDefault();
return;
}
// navigate with hover in horizontal mode
if (this.root) {
this.app.menuHoverActive = !this.app.menuHoverActive;
}
// notify other items
this.menuService.onMenuStateChange(this.key);
// execute command
if (this.item.command) {
this.item.command({ originalEvent: event, item: this.item });
}
// toggle active state
if (this.item.items) {
this.active = !this.active;
this.animating = true;
} else {
// activate item
this.active = true;
// reset horizontal menu
if (this.app.isHorizontal()) {
this.menuService.reset();
}
this.app.overlayMenuActive = false;
this.app.overlayMenuMobileActive = false;
this.app.menuHoverActive = !this.app.menuHoverActive;
}
}
onMouseEnter() {
// activate item on hover
if (this.root && this.app.menuHoverActive && this.app.isHorizontal() && this.app.isDesktop()) {
this.menuService.onMenuStateChange(this.key);
this.active = true;
}
}
onAnimationDone() {
this.animating = false;
}
ngOnDestroy() {
if (this.menuSourceSubscription) {
this.menuSourceSubscription.unsubscribe();
}
if (this.menuResetSubscription) {
this.menuResetSubscription.unsubscribe();
}
}
}
您的主机绑定应该返回布尔值(根据组件的状态计算),并且当前您正在为其分配一个字符串值。请记住,它是组件中打字稿文件的一部分,因此您可以访问所有属性。
因此,类似这样的方法应该有效:
@HostBinding('class.active-menuitem') get activeMenuItem(): boolean {
return (
(this.selectedKey && this.app.isHorizontal()) ||
(this.active && !this.app.isHorizontal()) ||
(this.active && !this.root && this.app.isHorizontal())
);
}
注意,在构造类的过程中,我不会赋值。相反,我使用了一个带有getter的属性。这意味着hostbinding将在每个更改检测周期更新。
本文描述了如何在同一个指令中实现多个选择器: https://www . benna del . com/blog/3367-matching-multiple-selectors-on-the-same-element-create-a-single-directive-instance-in-angular-5-0-0 . htm 但是当我定义或时,两者都是针对相同的指令。 能单独定义一下吗?
元数据属性 Subversion的一个新特性就是你可以对文件和目录任意附加元数据(或者是“属性”),属性是关联在工作拷贝文件或目录的任意名称/值对。 为了设置或得到一个属性名称,使用svn propset和svn propget子命令,列出对象所有的属性,使用svn proplist。 更多信息见“属性”一节。
问题内容: 我熟悉Java,但是刚开始学习JavaFX,尤其是学习JavaFX属性。我了解Oracle的以下示例所示的基本设计模式: 我不明白的是什么时候/为什么要使用getter和setter方法,而不是直接使用Property? 我在想的是,您可能希望在getter和/或setter中有一些自定义代码,这些代码可能对数据做一些前后操作/验证,但是如果您创建自定义getter和/或setter,
请尝试使用一个开关,而不是太多的if语句。只是为了让我的代码更易读。因为switch只计算一个变量,我能在我的代码中使用switch吗,因为它看起来有点复杂。一个建议,请,因为所有有不同的元素选择使用css选择器。 提前谢谢。
我们还可以通过在侦听器中添加目标来响应外部事件,例如从 或 document。 对于一个指令,这个概念是相当简单的。 将您的指令属性放在哪个模板标记被认为是Host元素。如果我们像上面这样实现HighlightDirective: 在组件的上下文中,Host元素是您通过组件配置中的选择器字符串创建的标记。 对于上面示例中的,组件类的上下文中的Host元素将是<my-text-box>标记。
本文向大家介绍为什么属性使用className而不是class呢?相关面试题,主要包含被问及为什么属性使用className而不是class呢?时的应答技巧和注意事项,需要的朋友参考一下 class是关键字 js会使用dom.className获取样式名,跟js保持一致