要解决Angular 6 innerHtml不绑定routerLink的问题,可以使用以下方法:
import { Directive, ElementRef, AfterViewInit } from '@angular/core';
@Directive({
  selector: '[appRouterLinkInnerHtml]'
})
export class RouterLinkInnerHtmlDirective implements AfterViewInit {
  constructor(private el: ElementRef) { }
  ngAfterViewInit() {
    const links = this.el.nativeElement.querySelectorAll('a');
    links.forEach((link: HTMLAnchorElement) => {
      const href = link.getAttribute('href');
      if (href && !link.getAttribute('routerLink')) {
        link.setAttribute('routerLink', href);
      }
    });
  }
}
在上述示例中,htmlContent 是一个包含 HTML 内容的字符串,它可能包含锚点元素。应用了 appRouterLinkInnerHtml 指令的 div 元素会解析 innerHtml 内容,并为其中的所有锚点元素添加 routerLink 属性。
这样,对于 innerHtml 中的锚点元素,就可以正常绑定到 Angular 的路由导航中了。