【问题标题】:Angular 6 -"generic type elementref requires 2 type arguments", couldn`t use itAngular 6 -“通用类型 elementref 需要 2 个类型参数”,不能使用它
【发布时间】:2018-10-16 15:33:42
【问题描述】:

我尝试使用 ElementRef,使用此导入:

import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';

我已尝试按照所有示例中的指示注入构造函数。

constructor(private formBuilder: FormBuilder, private elm: ElementRef) { }

它给了我一个错误:

“通用类型'elementref t any'需要2个类型参数”

也试过这样用:

@ViewChild("user") elmUser: ElementRef;

但这会导致同样的错误。

我该如何正确地做到这一点?

【问题讨论】:

  • 请显示更多代码...
  • 这是所有关于 ElementRef 的代码,请告诉我还有什么要分享的
  • 错误在其他地方。最好分享整个 ts 代码。
  • 他们改了,所以你必须写:ElementRef<HtmlElement>
  • 能否分享一个链接,例如 ElementRef

标签: angular


【解决方案1】:

ElementRef 需要 Type: T,因为它是 nativeElement 属性:

class ElementRef<T> {
  constructor(nativeElement: T)
  nativeElement: T
}

尝试像这样使用它:

constructor(
  private formBuilder: FormBuilder, 
  private elm: ElementRef<HTMLElement>
) { }

@ViewChild("user") elmUser: ElementRef<HTMLElement>;

它以前可以工作。但可能他们最近改变了它。

【讨论】:

  • 难以置信。尝试破坏您的服务器一次,然后尝试重建并提供您的应用程序。
  • 我的 IDE 是 Visual Studio,Visual Studio 中仍然存在错误
  • 这是我的参考代码,我的角度版本是 6
  • @mano10 但是ng serve 会抱怨吗?否则看起来 Visual Studio 可能是错误的。
  • ng serve 没有抱怨,但是当我尝试设置断点并检查 elementref 的属性时,它为空,所以,我认为确实有问题
猜你喜欢
  • 1970-01-01
  • 2020-07-08
  • 2017-12-07
  • 2018-11-13
  • 2017-01-09
  • 1970-01-01
  • 2020-10-14
  • 2016-06-17
  • 1970-01-01
相关资源
最近更新 更多