Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在ionic 5中搜索事件的备选方案_Angular_Typescript_Ionic Framework_Ionic5 - Fatal编程技术网

Angular 在ionic 5中搜索事件的备选方案

Angular 在ionic 5中搜索事件的备选方案,angular,typescript,ionic-framework,ionic5,Angular,Typescript,Ionic Framework,Ionic5,离子4组分: import {Events} from '@ionic/angular'; constructor(private auth: AuthService, private events: Events) {} intercept(req: HttpRequest<any>, next: HttpHandler) { // Refresh token failed, logout user this.events.publish('use

离子4组分:

import {Events} from '@ionic/angular';

 constructor(private auth: AuthService, private events: Events) {}




intercept(req: HttpRequest<any>, next: HttpHandler) {
     
 // Refresh token failed, logout user
    this.events.publish('user:logout', 'La sesión ha expirado');
    
    }

如何将参数添加到此事件服务?

首先为主题指定一个类型,然后在调用next()时传递一个参数,如下所示:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({providedIn: 'root'})

export class EventService{
  private formRefreshAnnouncedSource = new Subject<string>();
  formRefreshSource$ = this.formRefreshAnnouncedSource.asObservable();

  publishFormRefresh(eventName: string){
    this.formRefreshAnnouncedSource.next(eventName)
  }

}
从'@angular/core'导入{Injectable};
从'rxjs'导入{Subject};
@可注射({providedIn:'root'})
导出类事件服务{
private formRefreshAnnouncedSource=新主题();
formRefreshSource$=this.formRefreshAnnouncedSource.asObservable();
publishFormRefresh(事件名称:字符串){
this.formRefreshAnnouncedSource.next(eventName)
}
}
您可以在幕后使用主题构建自己的“事件服务”:

// app-events.enum.ts

export enum AppEvent {
  UserLogout = 'UserLogout',
  // ...
}

如果您需要更好地支持有效负载中的类型,我相信这可以得到改进,但它应该足以取代用于Ionic 3的Ionic事件。

这是否回答了您的问题?谢谢你的帮助…我是爱奥尼亚的新手,…先生,实际上我需要呼叫注销…我在login.ts文件中有用户注销按钮和方法…请告诉我如何在刷新令牌过期时使用事件注销。Thanks@Ajt当然你能用你的代码创建一个Stackblitz演示吗?如果需要,可以使用:)
// app-events.enum.ts

export enum AppEvent {
  UserLogout = 'UserLogout',
  // ...
}
// app-events.service.ts

import { Injectable } from '@angular/core';

import { Observable, Subject } from 'rxjs';
import { filter, map } from 'rxjs/operators';

import { AppEvent } from '../enums/app-event.enum';

interface EventDetails {
  event: AppEvent;
  payload?: unknown;
}

@Injectable({ providedIn: 'root' })
export class AppEventsService {
  private eventsDispatcher = new Subject<EventDetails>();

  public dispatch(event: AppEvent, payload?: unknown): void {
    this.eventsDispatcher.next({ event, payload });
  }

  public onEvent(event: AppEvent): Observable<unknown> {
    return this.eventsDispatcher.asObservable().pipe(
      filter((eventDetails) => eventDetails.event === event),
      map((eventDetails) => eventDetails.payload),
    );
  }
}

// dispatch
this.appEventsService.dispatch(AppEvent.UserLogout, 'La sesión ha expirado');

// listen to events
this.appEventsService
    .onEvent(AppEvent.UserLogout)
    .subscribe((message: string) => {
      console.log(message);
    });