简体   繁体   English

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

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

I have a angular 8 application.我有一个 angular 8 应用程序。 And there is a possibility to enter a link.并且有可能输入链接。

But you have two kind of links:但是你有两种链接:

internal and external.内部和外部。 with external I mean outside the domain of the site.与外部我的意思是在网站域之外。 For example: http://www.ad.nl例如: http : //www.ad.nl

Then if a user click on that link the user has to be redirected to other tab with that link.然后,如果用户单击该链接,则必须将用户重定向到具有该链接的其他选项卡。

But you can also have a internal link: like this: http://localhost:4200/gezondheid/Measurement/actieindex但是你也可以有一个内部链接:像这样: http://localhost:4200/gezondheid/Measurement/actieindex

But then the link has to be opening in the same tab.但是链接必须在同一个选项卡中打开。

I have a directive for it like this:我有一个这样的指令:

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

and I inject it in the component like this:我像这样将它注入到组件中:

 <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>

But it works for now fo intern link.但它现在适用于实习链接。 But if the link is: http://www.ad.nl then it will open in the same tab en not in a seperate tab.但是,如果链接是: http : //www.ad.nl,那么它将在同一个选项卡中打开,而不是在单独的选项卡中。

So what I have to change?那我必须改变什么?

Thank you谢谢

Well first off, with internal link, I guess you do not mean a link that's inside the angular app, because for that you have to use [routerLink] .首先,对于内部链接,我想您不是指 angular 应用程序内的链接,因为为此您必须使用[routerLink] If it's just to a different page outside of the angular app but inside the same domain, I believe you should do the following:如果它只是在 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);
  }
}

This basically means, don't use the attr.这基本上意味着,不要使用attr. binding, because that doesn't work in this case :) You need to update the property, not just the attribute.绑定,因为这在这种情况下不起作用:) 您需要更新属性,而不仅仅是属性。

stackblitz闪电战

I've created a simple example of how else you could solve this task我已经创建了一个简单的示例,说明您还可以如何解决此任务

The idea is to prevent default behavior of link tags and resolve routing via javascript.这个想法是为了防止链接标签的默认行为并通过 javascript 解析路由。

Also, you dont need to know about link type on every ngOnChanges fire.此外,您不需要知道每次 ngOnChanges 触发时的链接类型。 So you can check it only on click event and make decision where to route your customer.因此,您只能在点击事件时检查它并决定将客户路由到哪里。

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

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