【发布时间】:2020-10-26 11:37:26
【问题描述】:
我正在开发一个由多个团队在一段时间内开发的大型应用程序。
一位来自不同地点的前端开发人员添加了大量重复的命令式代码,并在应用程序中的任何地方(在每个组件中)使用 jquery 进行 dom 操作,以解决可访问性问题。
我正在尝试清理该代码。我将一些代码移到了指令中(例如:- 当元素被键盘聚焦时勾勒出轮廓,或者在模糊时移除轮廓)
这是我的指令。
import { Directive, HostListener, ElementRef, OnInit, Input, AfterViewInit } from '@angular/core';
import { AccessibilityService } from 'src/app/services/accessibility.service';
@Directive({
selector: '[keyboardFocus]'
})
export class KeyboardFocusDirective implements OnInit, AfterViewInit {
@Input('outlineOnFocusFor') outlineFor: 'self'|'parent'|'none'|null|undefined;
private readonly outlineStyleOnFocus = '1px solid black';
private tabindexChange: boolean;
private elemToOutine: any;
constructor(private el: ElementRef, private accessibilityService: AccessibilityService) { }
ngOnInit() {
this.accessibilityService.currentTabindexChange.subscribe((tabindexChange: boolean) => {
this.tabindexChange = tabindexChange;
if (this.el && this.el.nativeElement) {
this.el.nativeElement.tabIndex = this.tabindexChange ? -1 : 0;
}
});
}
ngAfterViewInit() {
switch (this.outlineFor) {
case 'parent':
this.elemToOutine = this.el.nativeElement.parentElement;
break;
case 'none':
this.elemToOutine = null;
break;
case 'self':
case null:
case undefined:
default:
this.elemToOutine = this.el.nativeElement;
break;
}
}
@HostListener('keyup.tab', ['$event'])
@HostListener('keyup.shift.tab')
onKeyboardTabFocus(event: KeyboardEvent) {
if (!this.elemToOutine)
return;
this.elemToOutine.style.outline = this.outlineStyleOnFocus;
event.stopImmediatePropagation();
}
@HostListener('blur', ['$event'])
onKeyboardBlur(event: KeyboardEvent) {
if (!this.elemToOutine)
return;
this.elemToOutine.style.outline = '';
event.stopImmediatePropagation();
}
}
这是我在虚构组件中使用它的方式。
<div class="something">
<section class="something-else" keyboardFocus outlineOnFocusFor="parent">some content 1</section>
<section class="something-else-2" keyboardFocus>some content 2</section>
<section class="something-else-3" keyboardFocus outlineOnFocusFor="none">some content 3</section>
</div>
这似乎在开发版本中运行良好。但是使用 --prod 标志构建的 CLI 会出现如下错误:
src/app/features/enrollments/ate-requests/ate-requests.component.html(73,17) 中的错误:: Directive KeyboardFocusDirective,需要 1 个参数,但得到 0。 src/app/features/enrollments/ate-requests/ate-requests.component.html(141,17): : Directive KeyboardFocusDirective,需要 1 个参数,但得到 0。 src/app/features/enrollments/ate-requests/ate-requests.component.html(156,23): : Directive KeyboardFocusDirective,需要 1 个参数,但得到 0。
使用该指令的所有地方都出现同样的错误。我无法弄清楚缺少的论点可能是什么。任何建议,将不胜感激。谢谢你:)
【问题讨论】:
标签: angular angular-cli angular-directive ng-build