angular - 我得到这个 ' Property ' 匹配'在类型 'EventTarget' 上不存在。'在 Angular-cli 输出上

标签 angular typescript events angular-cli

但是代码在 chrome 控制台上完成工作,没有错误

 constructor() {
    window.onclick = (event: MouseEvent) => {
      if (!event.target.matches('.dropbtn')) {
        const dropdowns = document.getElementsByClassName('dropdown-content');
        let i;
        for (i = 0; i < dropdowns.length; i++) {
          const openDropdown = dropdowns[i];
          if (openDropdown.classList.contains('show')) {
            openDropdown.classList.remove('show');
          }
        }
      }
    };
  }

我想有 typescript 的东西..有人拥有它吗?

最佳答案

更改事件类型以进行快速修复:

window.onclick = (event: any) => {

您还可以使用类型断言来告诉编译器它没问题:

window.onclick = (event: MouseEvent) => {
  const event_cast_type = event as any;
  if (!event_cast_type.target.matches('.dropbtn')) {

但请记住,类型断言与使用任何类型断言一样危险,因为您告诉编译器您比编译器更了解类型。然而,对于这种情况,我认为这是解决此问题的唯一方法,因为类型断言将从根本上解决问题,即编译器。

关于angular - 我得到这个 ' Property ' 匹配'在类型 'EventTarget' 上不存在。'在 Angular-cli 输出上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634155/

相关文章:

c# - 如何在用户控件中使用委托(delegate)和事件?

angular - auth0 授权 api 调用返回 200 状态但仍然得到错误响应

javascript - 将列表转换为字典

javascript - typescript 方法覆盖不起作用

Angular Material 卡可点击

spring - ApplicationEventPublisher 没有被 Autowiring 到组件中

android - 父进程停止时停止子进程

angular - 包括 ionic 2/angular 2 的公共(public)标题栏

angular - 是否有用于音频管理的 Angular 4 npm 包?

typescript - 具有抽象语法的 Visual Studio 代码