只获得字符串的最后一个字母

时间:2018-06-12 17:54:14

标签: angular rxjs ngrx

我正在使用NgRX 6和Angular 6。

我有一个调用服务的效果,该服务返回一个字符串。

出于某种原因,我只收到字符串的最后一个字母。

这是我的效果

  @Effect()
  identityRedirect$ = this.actions$.pipe(
    ofType(REDIRECT_TO_LOGIN),
    switchMap(() =>
      forkJoin(this.discoverService.getServiceUrl('{api:identity}'), this.discoverService.getServiceUrl('{site:self}'))
    ),
    mergeMap(([x, y]: [string, string]) => {
      console.log(x);
      console.log(y);
      return of({ type: REDIRECT_TO_LOGIN_SUCCESS, payload: 'foo' });
    })
  );

我认为forkJoin会有所帮助,但我在控制台日志中看到的只是最后一封信

我的发现服务看起来像这样

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

@Injectable()
export default class DiscoverService {
  constructor() {}

  public getServiceUrl = (service: string) => {
    const { domain, endpoint } = this.splitServiceObject(service);
    return endpoints[domain][endpoint]['uri'];
  }

  private splitServiceObject = (service: string) => {
    const moduleParams = service.split(/{(.*)}/);
    const [domain, endpoint] = moduleParams[1].split(':');
    return { domain, endpoint };
  }
}

1 个答案:

答案 0 :(得分:0)

所以我改变了效果,看起来像这样

  @Effect()
  identityRedirect$ = this.actions$.pipe(
    ofType(REDIRECT_TO_LOGIN),
    switchMap(() =>
      combineLatest(
        this.discoverService.getServiceUrl('{api:identity}'),
        this.discoverService.getServiceUrl('{site:self}')
      )
    ),
    mergeMap(([auth, redirect]: [string, string]) => {
      console.log(auth);
      console.log(redirect);
      return of({ type: REDIRECT_TO_LOGIN_FAILURE, payload: 'error' });
    })
  );

我还需要像这样更新我的服务

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

import endpoints from './endpoints';
import { map } from 'rxjs/operators';

@Injectable()
export default class DiscoverService {
  constructor() {}

  public getServiceUrl = (service: string): Observable<string> => {
    return this.splitServiceObject(service).pipe(map(([domain, endpoint]) => endpoints[domain][endpoint]['uri']));
  }

  private splitServiceObject = (service: string): Observable<any> => {
    const moduleParams = service.split(/{(.*)}/);
    // const [domain, endpoint] = moduleParams[1].split(':');
    return of(moduleParams[1].split(':'));
  }
}