我遇到了这个问题:
我无法访问 HTML 元素的 href 属性。 我收到此错误:
Property 'href' does not exist on type 'HTMLElement'.ts(2339)
这是代码:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
title = 'clientes-app';
ngAfterViewInit(){
(function($) {
"use strict";
var path = window.location.href;
$("#layoutSidenav_nav .sb-sidenav a.nav-link").each(function() {
if (this.href === path) {
$(this).addClass("active");
}
});
// Toggle the side navigation
$("#sidebarToggle").on("click", function(e) {
e.preventDefault();
$("body").toggleClass("sb-sidenav-toggled");
});
})(jQuery);
}
}
最佳答案
并非所有 HTMLElement
都有 href
属性。在访问之前检查当前迭代的元素是否是具有 href
属性的 HTMLAnchorElement
。
if (this instanceof HTMLAnchorElement && this.href === path) {
$(this).addClass("active");
}
关于angular - TS2339 : Property 'href' does not exist on type 'HTMLElement' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65745283/