angular - “元素”类型上不存在属性“onclick”
问题描述
我有以下角度组件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-status',
templateUrl: './status.component.html',
styleUrls: ['./status.component.css']
})
export class StatusComponent implements OnInit {
constructor() { }
ngOnInit() {
const acc = document.getElementsByClassName('accordion');
let i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle('active');
this.nextElementSibling.classList.toggle('show');
};
}
}
}
我正在从编译器中检索以下错误
src/app/components/status/status.component.ts(16,16) 中的错误:错误 TS2339:“元素”类型上不存在属性“onclick”。
尽管编译是成功的,并且所有工作都按预期进行。
我应该忽略错误吗?
解决方案
您需要将其转换为HTMLElement
const acc: HTMLElement = document.getElementsByClassName('accordion') as HTMLElement;
推荐阅读
- c++ - 从 ffmpeg 1.1 升级到 3.3 后没有帧解码
- ios - 在 wifi 网络 iOS 中扫描连接的设备
- docusignapi - Docusign 标签值被错误地应用于其他标签
- javascript - 如何将序列化数组发送到 php 文档?
- c# - 从文件共享中读取图像路径并存储在表存储中 azure
- linux - ImageMagick 没有解码委托
- mysql - SQL 查询没有值
- telegram - 用电报机器人显示表格?
- sql - 如何在 SAP HANA 1.2v 中实现层次结构或递归 CTE
- c# - 并行发出多个请求时,HttpClient 花费过多的时间(除非 Http 版本设置为 1.0)