当前位置: 首页 > 知识库问答 >
问题:

类型“EventTarget”上不存在属性“value”

穆宏胜
2023-03-14

e.target.value.match(/\s+/g)[]).length

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

@Component({
  selector: 'text-editor',
  template: `
    <textarea (keyup)="emitWordCount($event)"></textarea>
  `
})
export class TextEditorComponent {
  @Output() countUpdate = new EventEmitter<number>();

  emitWordCount(e: Event) {
    this.countUpdate.emit(
            (e.target.value.match(/\S+/g) || []).length);
  }
}

共有1个答案

隗驰
2023-03-14

您需要明确告诉TypeScript作为目标的HTMLElement的类型。

方法是使用泛型类型将其转换为适当的类型:

this.countUpdate.emit((<HTMLTextAreaElement>e.target).value./*...*/)

或者(如你所愿)

this.countUpdate.emit((e.target as HTMLTextAreaElement).value./*...*/)
const target = e.target as HTMLTextAreaElement;

this.countUpdate.emit(target.value./*...*/)
// create a new type HTMLElementEvent that has a target of type you pass
// type T must be a HTMLElement (e.g. HTMLTextAreaElement extends HTMLElement)
type HTMLElementEvent<T extends HTMLElement> = Event & {
  target: T; 
  // probably you might want to add the currentTarget as well
  // currentTarget: T;
}

// use it instead of Event
let e: HTMLElementEvent<HTMLTextAreaElement>;

console.log(e.target.value);

// or in the context of the given example
emitWordCount(e: HTMLElementEvent<HTMLTextAreaElement>) {
  this.countUpdate.emit(e.target.value);
}
 类似资料:
  • 所以下面的代码是在Angular 4中,我不知道为什么它没有按预期的方式工作。 下面是我的处理程序的一个片段: 超文本标记语言元素: 代码给了我错误: 属性值在类型Event塔吉特上不存在。 但是从< code>console.log中可以看出,该值确实存在于< code>event.target中。

  • 我使用的是完全修补过的Visual Studio 2013。我正在尝试使用JQuery、JQueryUI和JSRender。我也在尝试使用打字。在ts文件中,我得到如下错误: 类型“{}”上不存在属性“fade div”。

  • 我试图在登录过程后获得连接的用户模型, 首先,在CanActivate guard检查了带有用户JSON对象的本地存储并发现该对象为空之后,用户将重定向到“/login”URL 然后用户登录并重定向回根URL“/”,在连接过程中我使用AuthService,它将从服务器返回的用户对象保存在用户模型中 这是我的守卫:

  • 升级到Angular 6.0和Rxjs到6.0后,我收到以下编译错误:

  • 获取错误:(类型窗口中不存在属性“MktoForms2”

  • 我试图在TypeScript中使用“时刻-持续时间格式”,但即使它有效,webpack仍不断抱怨它无法在线找到“格式”方法: 这里是package.json tsconfig.json: 在(angular2)组件中: 我也试过了 但这并不能改变什么: [at-loader]./src/app/组件/建筑/商店/shop.component.ts:190中的错误:81 TS2339:属性“格式”在