Complete the code to create an effect that logs a message when an action is dispatched.
readonly logEffect = createEffect(() => this.actions$.pipe(tap(() => console.log([1]))));The tap operator is used here to perform a side effect: logging a message. The message string should be passed inside console.log.
Complete the code to create a non-dispatching effect that shows an alert when the 'loginSuccess' action occurs.
readonly alertEffect = createEffect(() => this.actions$.pipe(ofType('loginSuccess'), tap(() => alert([1]))), { dispatch: [2] });
The alert function shows a message. The message should be a string like 'Login successful!'. The dispatch option should be set to false to indicate no new action is dispatched.
Fix the error in the effect that should log the action type but currently does not work.
readonly logActionType = createEffect(() => this.actions$.pipe(tap(action => console.log(action.[1]))));Actions have a type property that describes the action name. Logging action.type shows which action was dispatched.
Fill both blanks to create an effect that listens for 'logout' action and navigates to the login page without dispatching a new action.
readonly logoutEffect = createEffect(() => this.actions$.pipe(ofType([1]), tap(() => this.router.[2]('/login'))), { dispatch: false });
The effect listens for the 'logout' action. To navigate programmatically, router.navigateByUrl is used to go to the '/login' page. The effect does not dispatch a new action.
Fill all three blanks to create an effect that listens for 'loadData' action, calls a service method, and logs the result without dispatching a new action.
readonly loadDataEffect = createEffect(() => this.actions$.pipe(ofType([1]), switchMap(() => this.dataService.[2]().pipe(tap(result => console.log([3]))))), { dispatch: false });
The effect listens for the 'loadData' action. It calls the service method fetchData which returns an observable. The result is logged inside tap using the variable result. The effect does not dispatch a new action.