繁体   English   中英

如何在 angular 中使用外部链接指令?

[英]How to use directive for external link in angular?

我有一个 angular 8 应用程序。 并且有可能输入链接。

但是你有两种链接:

内部和外部。 与外部我的意思是在网站域之外。 例如: http : //www.ad.nl

然后,如果用户单击该链接,则必须将用户重定向到具有该链接的其他选项卡。

但是你也可以有一个内部链接:像这样: http://localhost:4200/gezondheid/Measurement/actieindex

但是链接必须在同一个选项卡中打开。

我有一个这样的指令:

@Directive({
  selector: '[appExternalLink]'
})

/**
 * This directive is responsible for checking if in Activity the link is a external link - then it wil open
 * in a seperate tab or is the link an internal(same domain) link, then it will open in the same tab
 */
export class ExternalLinkDirective {

  @HostBinding('attr.rel') relAttr = '';
  @HostBinding('attr.target') targetAttr = '';
  @HostBinding('attr.href') hrefAttr = '';
  @Input() href: string;

  /**
   * Check if liink is external or not
   */
  ngOnChanges() {
    this.hrefAttr = this.href;

    if (this.isLinkExternal()) {
      this.relAttr = 'noopener';
      this.targetAttr = '_blank';
    }
  }

  /**
   * if link is external, then set the rel and target attributes
   */
  private isLinkExternal() {
    return !this.href.includes(location.hostname);
  }

我像这样将它注入到组件中:

 <a appExternalLink
      *ngIf="activity.link; else nolink"
      [href]="activity.link">
      <div class="todo-day-part-subject">{{ activity.subject }}</div>
      <div class="todo-day-part-block">
        <div class="todo-day-part-icon {{ getIcon(activity.type) }}"></div>
        <div class="todo-day-part-content">
          <div class="todo-day-part-text">{{ activity.text }}</div>
          <div class="todo-day-part-time">{{ activity.beginDate | date: 'HH:mm' }}</div>
          <div *ngIf="activity.link" class="todo-day-part-readmore">Bekijk</div>
          <div *ngIf="activity.state === 'Done'" class="todo-day-part-status-signal">
            <span class="fa fa-check-circle"></span>
          </div>
        </div>
      </div>
    </a>

但它现在适用于实习链接。 但是,如果链接是: http : //www.ad.nl,那么它将在同一个选项卡中打开,而不是在单独的选项卡中。

那我必须改变什么?

谢谢

首先,对于内部链接,我想您不是指 angular 应用程序内的链接,因为为此您必须使用[routerLink] 如果它只是在 angular 应用程序之外但在同一域内的不同页面,我相信您应该执行以下操作:

@Directive({
  selector: 'a[appExternalLink]'
})
export class ExternalLinkDirective {

  @HostBinding('rel')
  rel = '';

  @HostBinding('target')
  target = '_self';

  @Input()
  @HostBinding('href')
  href?: string;

  ngOnChanges(changes: SimpleChanges) {
    if (changes.href) {
      const isExternal = this.isLinkExternal();
      this.rel = isExternal ? 'noopener' : '';
      this.target = isExternal ? '_blank' : '_self';
    }
  }

  /**
   * Check if link is external or not
   */
  private isLinkExternal() {
    return !(this.href || '').includes(location.hostname);
  }
}

这基本上意味着,不要使用attr. 绑定,因为这在这种情况下不起作用:) 您需要更新属性,而不仅仅是属性。

闪电战

我已经创建了一个简单的示例,说明您还可以如何解决此任务

这个想法是为了防止链接标签的默认行为并通过 javascript 解析路由。

此外,您不需要知道每次 ngOnChanges 触发时的链接类型。 因此,您只能在点击事件时检查它并决定将客户路由到哪里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM