Javascript 如何在读取第二个值之前禁用NFC EventListener?

Javascript 如何在读取第二个值之前禁用NFC EventListener?,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,这个问题来自NFC标签太敏感。 当我放置NFC标签一次时,会立即输入太多的值。 因此,如果输入了一个NFC标记值,则希望在该时刻停止NFC事件侦听器。我如何实现它 我认为设备和NFC标签之间的NFC读取速度比程序端的EventListener取消快得多 此爱奥尼亚页面的工作方式如下… *1) 第一次加载IonViewDiCenter() 2) 添加addListenNFC() 3) 如果NFC已标记,则将值发送到onNfcLogin(tagId) 4) 在onNfcLogin(tagId)中,它

这个问题来自NFC标签太敏感。 当我放置NFC标签一次时,会立即输入太多的值。 因此,如果输入了一个NFC标记值,则希望在该时刻停止NFC事件侦听器。我如何实现它

我认为设备和NFC标签之间的NFC读取速度比程序端的EventListener取消快得多

此爱奥尼亚页面的工作方式如下…

*1) 第一次加载IonViewDiCenter()

2) 添加addListenNFC()

3) 如果NFC已标记,则将值发送到onNfcLogin(tagId)

4) 在onNfcLogin(tagId)中,它作为userService.nfclogin()包含在“HTTPPOST服务提供者”中

5) 最后从服务器端获取json类型的返回*

{ “依赖项”:{ “离子原生/核心”:“4.7.0”, “离子角”:“3.9.2”, “类型脚本”:“~2.6.2”] }, “测试设备”:“Galaxy8”, “NFC标签”:“便宜的NFC棒” }

  ionViewDidEnter() {
    this.nfc.enabled().then((resolve) => {
      this.addListenNFC();
    }).catch((reject) => {
      alert("NFC is not supported by your Device");
    });
  }


  addListenNFC() {
    this.nfc.addTagDiscoveredListener().subscribe(data => {
    //window.removeEventListener; //this is not working.
      if (data && data.tag && data.tag.id) {
        this.tagId = this.nfc.bytesToHexString(data.tag.id);
        if (this.tagId) {
          this.scanned = true;
          this.onNfcLogin(this.tagId);
        } else {
          alert('NFC_NOT_DETECTED');
        }
      }
    });
  }


  onNfcLogin(tagId) {
    this.userService.nfclogin(tagId).subscribe(data => { 
       // "this.userService.nfclogin()" is at Http post service provider
      this.getData = JSON.stringify(data);
      if (this.getData) {
        this.granted = true;
        this.loading.dismiss();
        this.events.publish('user:login');
        this.navCtrl.setRoot(HomePage);
      }
      this.resetScanData;
    },
      error => {
        this.loading.dismiss();
        this.showError(error);
      });
  }


  showError(error) {
    console.log(error);
    let alert = this.alertCtrl.create({
      title: 'Login Error',
      message: error.json().message,
      buttons: ['OK']
    });
    alert.present();
  }


  resetScanData() {
    this.granted = false;
    this.scanned = false;
    this.tagId = "";
  }

您只能使用observable.take(1)订阅第一个订阅。订阅(…)。

然后再重新创建订阅。

我用这段代码解决了这个问题

this.myListener = this.nfc.addNdefListener(() => {
   console.log(‘successfully attached ndef listener’);
},(err) => {
   console.log(‘error attaching ndef listener’, err);
}).subscribe((event) => {
  . . .
});

//remove listener
this.myListener.unsubscribe();
感谢爱奥尼亚论坛的Avdm。