Ngrx @Effects - 立即停止
Ngrx @Effects - Make it stop immediately
我有下面的ngrx效果。 "map"里面我有个条件。如何不继续mergeMap就立即停止效果
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
map((action: any) => {
const url = 'http://www.academy.xyzq';
if (1 === 1) {
// Make this effect stop immediately and do nothing more
}
action.url = url;
return action;
}),
mergeMap((action: any) =>
this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
You can try like this.
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
map((action: any) => {
const url = 'http://www.academy.xyzq';
if (1 === 1) {
// Make this effect stop immediately and do nothing more
return null;
}
action.url = url;
return action;
}),
mergeMap((action: any) =>
if(action){
return this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
} else{
return null;
}
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
您应该考虑使用 filter
函数根据您的条件过滤掉操作:
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
filter((x) => 1 === 1),
mergeMap((action: any) =>
this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
或者,如果您的条件为真,您可以只分派一个新操作,并创建一个更简单的效果来执行 mergeMap
函数中的部分。
你应该尽量让你的效果尽可能简单。如果您的效果进行大量逻辑/条件检查,则很难找到一个合适的名称来描述效果的实际作用。
我有下面的ngrx效果。 "map"里面我有个条件。如何不继续mergeMap就立即停止效果
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
map((action: any) => {
const url = 'http://www.academy.xyzq';
if (1 === 1) {
// Make this effect stop immediately and do nothing more
}
action.url = url;
return action;
}),
mergeMap((action: any) =>
this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
You can try like this.
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
map((action: any) => {
const url = 'http://www.academy.xyzq';
if (1 === 1) {
// Make this effect stop immediately and do nothing more
return null;
}
action.url = url;
return action;
}),
mergeMap((action: any) =>
if(action){
return this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
} else{
return null;
}
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
您应该考虑使用 filter
函数根据您的条件过滤掉操作:
@Effect()
myEffect$: Observable < Action > = this.actions$.pipe(
ofType('INIT_ACADEMY_DOMAIN'),
filter((x) => 1 === 1),
mergeMap((action: any) =>
this.http.get(action.url).pipe(
switchMap((data: any) => {
return [{
type: 'INIT_ACADEMY',
payload: {
academy: data.academy
}
}];
})
)),
catchError(() => of ({
type: 'INIT_IT_FAILED'
}))
);
或者,如果您的条件为真,您可以只分派一个新操作,并创建一个更简单的效果来执行 mergeMap
函数中的部分。
你应该尽量让你的效果尽可能简单。如果您的效果进行大量逻辑/条件检查,则很难找到一个合适的名称来描述效果的实际作用。