angular - 只获取字符串的最后一个字母
问题描述
我正在使用 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' });
})
);
我认为 aforkJoin
会有所帮助,但我似乎在控制台日志中得到的只是最后一个字母
我的发现服务看起来像这样
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 };
}
}
解决方案
所以我改变了我的效果看起来像这样
@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(':'));
}
}
推荐阅读
- amazon-web-services - 如何将整个 s3 存储桶转移到 Glacier
- python - 使用 Numba 和 CUDA 对数组求和
- linux - bash 变量似乎未能分配给在容器中执行的 psql 的输出
- laravel - Laravel:如何保存从前端发送的对象数组?
- flutter - 如何使用 Visual Studio Code IntelliSense 显示 Flutter Widget 文档?
- c# - 反序列化 JSON 数组与列表
- python - Django:为什么我的 setttings.py 和 urls.py 中需要这些
- python - 从 Crontab 开始的简单 Huey 示例的挑战
- javascript - 如何将键数组转换为对象?
- python - 使用 Numpy.Load - 这是最快的方法吗?好像很慢