angular - Angular 结构指令上下文模板类型检查
问题描述
我似乎无法使角度模板自动完成工作,谁能帮我理解我错过了什么或做错了什么?
<div *ngVar="ok as xd">
<!-- xd is not giving any autocomplete / typecheck, hover giving "any" type -->
{{ xd.item }}
</div>
我几乎一直在研究 ngIf 和异步管道源代码,试图了解发生了什么,并制定了这个简单的指令,只是为了方便我的异步订阅生活(这对 ngIf 来说有点痛苦。
没有类型安全同样是疼痛x)
type NgVarContext<T> = { ngVar: T; $implicit: undefined };
@Directive({
selector: "[ngVar]",
})
export class NgVar<T> {
context: NgVarContext<T> = { ngVar: undefined, $implicit: undefined };
_subscription: Subscription;
constructor(
private cdr: ChangeDetectorRef,
template: TemplateRef<NgVarContext<T>>,
viewContainer: ViewContainerRef
) {
viewContainer.createEmbeddedView(template, this.context);
}
@Input()
set ngVar(input: Observable<T>) {
if (this._subscription) return;
this._subscription = input.subscribe((item) => {
if (item !== this.context.ngVar) {
this.context.ngVar = item;
this.cdr.markForCheck();
}
});
}
static ngTemplateGuard_ngVar: "binding";
static ngTemplateContextGuard<T>(dir: NgVar<T>, ctx: NgVarContext<T>): ctx is NgVarContext<T> {
return true;
}
}
解决方案
如果你不知道(写这个作为答案,因为我不能评论 <50 的声誉):如果你在 vscode 中启用实验性的常春藤语言服务,这已经对我有用:基本上与此处编写的实现相同。
推荐阅读
- dataflow-diagram - 您可以在数据流程图上使用读/写吗?
- sql - SQL - 新客户保留 - MoM
- excel - 使用少量元素副本获取 ComboBox.ListIndex
- windows - 在 GPO 中管理 Windows 10 通知
- python - 从 python 转换为 csv 时缺少 excel 中的第一列
- android - Gradle 错误显示“无法解析配置 ':app:debugRuntimeClasspath' 的所有文件。”
- sql - SQL:用反斜杠转义
- python - 使用自动操作,使用另一个字段的值(在销售订单上)更新字段的值(在交货单上)
- c# - 异步方法在控制台应用程序中无法正常工作
- reactjs - 在子组件中使用状态设置器功能