简体   繁体   English

EventListener不会触发

[英]EventListener doesn't trigger

I updated my ionic3 to ionic4 and hyperlinks aren't working anymore. 我将ionic3更新为ionic4,超链接不再起作用。 So I tried to set a new ClickEvent for them. 因此,我尝试为他们设置一个新的ClickEvent。 Unfortunately the click event doesn't work. 不幸的是,点击事件无效。 The content of my event is never reached even If I click some link. 即使单击某些链接,也永远无法到达活动的内容。 I don't get why it's not working. 我不明白为什么它不起作用。

 ngAfterViewChecked() { if (!this._element) return; let arrUrls = this._element.nativeElement.querySelectorAll('a'); console.log("arrUrls.length:", arrUrls.length); // Reached and correct console.log("arrUrls:", arrUrls); // Reached and correct this.setEventListener(arrUrls); } private setEventListener(arrUrls) { arrUrls.forEach((objUrl) => { console.log('5412212112121212'); // Reached console.log(objUrl); // Reached and correct // Listen for a click event on each hyperlink found objUrl.addEventListener('click', (event) => { //NOT REACHED event.preventDefault(); alert('7213983728912798312231'); // Isn't reached //this._link = event.target.href; }, false); }); } 

You don't really need to get so detailed and write this all yourself. 您实际上并不需要如此详细地自己编写。

Other people have already solved the problem such as the LinkifyJS project. 其他人已经解决了该问题,例如LinkifyJS项目。

There is an angular compatible wrapper for it: 有一个兼容的角度包装器:

Which, once you have done the standard setup process you can use it as either a pipe: 完成标准设置过程后,可以将其用作管道:

<span [innerHTML]="'Linkify the following URL: https://github.com/anthonynahas/ngx-linkifyjs and share it <3' | linkify"></span>

Or as a service: 或作为服务:

import {NgxLinkifyjsService, Link, LinkType, NgxLinkifyOptions} from 'ngx-linkifyjs';

constructor(public linkifyService: NgxLinkifyjsService) {

  const options: NgxLinkifyOptions =
     {
      className: 'linkifiedYES',
      target : {
          url : '_self'
        }
      };

  this.linkifyService.linkify('For help with GitHub.com, please email support@github.com');
  // result 1 --> see below

  this.linkifyService.linkify('For help with GitHub.com, please email support@github.com', options);
    // result 2 --> see below
 } 
}

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

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