打字稿错误:TS2314:通用类型“ElementRef<T, any>"需要 2 个类型参数 [英] Typescript Error: TS2314: Generic type &#39;ElementRef&lt;T, any&gt;&#39; requires 2 type argument(s)

查看:30
本文介绍了打字稿错误:TS2314:通用类型“ElementRef<T, any>"需要 2 个类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Angular 5.x 模板,但我使用 https://update 将其升级到 Angular 6.angular.io/ 指南.现在我的构造函数中有这个错误

I'm using an Angular 5.x template, but I upgrade it to Angular 6 using https://update.angular.io/ guide. Now I have this error in my constructor

Typescript Error: TS2314: Generic type 'ElementRef<T, any>' requires 2 type argument(s)

我的代码:

import { Component, Input, Output, EventEmitter, ElementRef, HostListener }          from '@angular/core';

@Component({
  selector: 'sidebar',
  templateUrl: './sidebar.component.html'
})

export class SidebarComponent {

    @HostListener('document:click', ['$event'])
  clickout(event) {
    if(!this.eRef.nativeElement.contains(event.target)) {
          this.hideMobileSidebar.emit(true);
    }
  }

  constructor(private eRef: ElementRef) {
  }

...

我在以前的 Angular 版本 5 中没有这个错误.

I don't have this error in previous Angular version 5.

有什么变化?我不明白文档 :( https://angular.io/api/core/ElementRef

What was the change? I don't understand the docs :( https://angular.io/api/core/ElementRef

推荐答案

我解决了问题.

我所做的只是运行 npm i @angular-cli --save 并且它从版本 6.0.7 更改为 6.0.8,我还对其进行了全局更新.然后我运行 ng update @angular/cli 但它不会改变我的 package.json 中的任何内容.现在我可以单独使用 ElementRef,或者 ElementRefElementRef,现在一切正常.我不明白 angular-cli 与我的 tslint 或 typescript 安装有什么关系,但这是我唯一做的事情.

What I did was just run npm i @angular-cli --save and it changed from version 6.0.7 to 6.0.8, I also updated it globally. Then I run ng update @angular/cli but it doesn't change anything in my package.json. Now I can use ElementRef alone, or ElementRef<HTMLElement> or ElementRef<HTMLElement, any>, EVERYTHING WORKS NOW. I don't understand what angular-cli has to do with my tslint or typescript install, but thats the only thing I did.

这篇关于打字稿错误:TS2314:通用类型“ElementRef<T, any>"需要 2 个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆