打字稿错误:TS2314:通用类型'ElementRef< T,any>'需要2个类型参数 [英] Typescript Error: TS2314: Generic type 'ElementRef<T, any>' requires 2 type argument(s)

查看:226
本文介绍了打字稿错误: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中的任何内容.现在,我可以单独使用ElementRefElementRef<HTMLElement>ElementRef<HTMLElement, any>了,现在一切正常.我不了解tslint或打字稿安装与angular-cli有什么关系,但这是我唯一要做的事情.

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&lt; T,any&gt;'需要2个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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