0

この問題が発生しています:

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);
  }
}
4

2 に答える 2

5

すべてHTMLElementの にhref属性があるわけではありません。アクセスする前に、現在反復されている要素が属性HTMLAnchorElementを持つかどうかを確認してください。href

if (this instanceof HTMLAnchorElement && this.href === path) {
     $(this).addClass("active");
}
于 2021-01-16T00:36:55.250 に答える