Is it possible to put createEffect() inside functions? #3743
-
Hello there, I'm new to rxjs and ngrx and I have some codes works, and I found that everyone writes "createEffect" at the property area of a Class and I thought it should also work if I wrote it inside a function, and it turned out I was wrong. Can anybody help me to understand what's the magic here? Working Code: @Injectable()
export class UserEffects {
loadUser$ = createEffect(() => this.actions$.pipe(
ofType(getUser),
first(),
mergeMap(() => {
return this.userService.getUser()
.pipe(
map(user => retievedUser({ user })),
catchError(() => EMPTY)
)
})
)
);
constructor(
private actions$: Actions,
private userService: UserService
) {
}
} Non-Working code: @Injectable()
export class UserEffects {
constructor(
private actions$: Actions,
private userService: UserService
) {
createEffect(() => this.actions$.pipe(
ofType(getUser),
first(),
mergeMap(() => {
return this.userService.getUser()
.pipe(
map(user => retievedUser({ user })),
catchError(() => EMPTY)
)
})
)
);
}
} I tried to learn https://github.com/ngrx/platform/blob/master/modules/effects/src/effect_creator.ts but I can't find anything special. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For now, effects can only be registered when they're assigned to a property. |
Beta Was this translation helpful? Give feedback.
For now, effects can only be registered when they're assigned to a property.
Keep an eye out for #3669, which adds functional effects.