in Education by
I tried to implement Ngrx into my Angular Application, but sadly haven't been able to retrieve any data. Any help is appreciated! What I'm trying to do in the property.component.ts is to get all the data when the component gets instantiated and just display the data in the property.component.html. property.component.ts import {Component, OnInit} from '@angular/core'; import {select, Store} from '@ngrx/store'; import {Router} from '@angular/router'; @Component({ selector: 'app-property', templateUrl: './property.component.html', styleUrls: ['./property.component.css'] }) export class PropertyComponent implements OnInit { properties$ = this._store.pipe(select('properties')); constructor(private _store: Store, private _router: Router) {} ngOnInit() { this._store.dispatch({ type: '[Property] Get Properties' }); console.log(this.properties$); } navigateToProperty(id: number) { this._router.navigate(['property', id]); } } property.component.html

test

{{properties$ | async}}

property.service.ts import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import { Observable } from 'rxjs'; import {PropertyModel} from '../models/property.model'; @Injectable({ providedIn: 'root' }) export class PropertyService { propertiesUrl = 'someapi'; private httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; constructor(private http: HttpClient) { } getProperties(): Observable { return this.http.get(this.propertiesUrl, this.httpOptions); } } property.actions.ts import { Action } from '@ngrx/store'; import {PropertyModel} from '../../models/property.model'; export const GET_PROPERTIES = '[Property] Get Properties'; export const GET_PROPERTIES_SUCCESS = '[Property] Get Properties Success'; export const SEARCH_PROPERTY = '[Property] Get Property'; export const SEARCH_PROPERTY_SUCCESS = '[Property] Get Property Success'; export class GetProperties implements Action { public readonly type = GET_PROPERTIES; constructor(public retrievedProperties: PropertyModel[]) {} } export class GetPropertiesSuccess implements Action { public readonly type = GET_PROPERTIES_SUCCESS; constructor(public retrievedProperties: PropertyModel[]) {} } export class GetProperty implements Action { public readonly type = SEARCH_PROPERTY; constructor(public searchId: string) {} } export type PropertyActions = GetProperties | GetPropertiesSuccess | GetProperty; property.effects.ts import {Injectable} from '@angular/core'; import {Actions, Effect, ofType} from '@ngrx/effects'; import {GET_PROPERTIES, PropertyActions} from '../actions/property.actions'; import {PropertyService} from '../../services/property.service'; import {mapTo} from 'rxjs/operators'; @Injectable() export class PropertyEffects { @Effect() getProperties$ = this.actions$.pipe( ofType(GET_PROPERTIES), mapTo(this.propertyService.getProperties()) ); constructor(private actions$: Actions, private propertyService: PropertyService) {} } and finally my property.reducers.ts import {GET_PROPERTIES, GET_PROPERTIES_SUCCESS, PropertyActions} from '../actions/property.actions'; import {PropertyModel} from '../../models/property.model'; export function propertyReducer(properties: PropertyModel[] = [], action: PropertyActions): PropertyModel[] { switch (action.type) { case GET_PROPERTIES: { return [...action.retrievedProperties]; } default: { return properties; } } } app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; import {AppComponent} from './app.component'; import {CustomerComponent} from './customer/customer.component'; import {HttpClientModule} from '@angular/common/http'; import {CustomMaterialModule} from './custom.material.module'; import {RouterModule, Routes} from '@angular/router'; import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; import {StoreModule} from '@ngrx/store'; import {PropertyComponent} from './property/property.component'; import {propertyReducer} from './store/reducers/property.reducers'; const appRoutes: Routes = [ { path: 'customers', component: CustomerComponent, data: {title: 'Customer List'} }, { path: 'property', component: PropertyComponent, data: {title: 'Property List'} }, { path: '', // General redirect component: CustomerComponent, pathMatch: 'full' } ]; @NgModule({ declarations: [ AppComponent, CustomerComponent, PropertyComponent ], imports: [ BrowserModule, RouterModule.forRoot(appRoutes), HttpClientModule, BrowserAnimationsModule, CustomMaterialModule, StoreModule.forRoot(propertyReducer) ], providers: [], bootstrap: [AppComponent] }) export class AppModule {} JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Working stackblitz example Don't forget to dispatch success and error actions and to reduce them. Also change mapTo to switchMapTo @Effect() getProperties$ = this.actions$.pipe( ofType(GET_PROPERTIES), switchMapTo(this.propertyService.getProperties().pipe( map(props => new GetPropertiesSuccess(props)), // catch error here // catchError(error => of(new GetPropertiesError(error))), ), ), ); In your reducer, listen to correct type case GET_PROPERTIES_SUCCESS: { return [...action.retrievedProperties]; } For clarity remove retrievedProperties from GetProperties - the action does not have any retrieved props. The success action should be used to pass them. In your app module change the reducer setup and don't forget to import the effects: imports: [ BrowserModule, RouterModule.forRoot(appRoutes), HttpClientModule, BrowserAnimationsModule, // change this StoreModule.forRoot({properties: propertyReducer}), // this is needed EffectsModule.forRoot([PropertyEffects]) ], Optional: In your PropertyComponent change the the dispatched action to the instance of the GetProperties class. That's how it should be used. this._store.dispatch(new GetProperties());

Related questions

0 votes
    I have the following table relationship in my database: Parent / \ Child1 Child2 \ / GrandChild I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the Hateoas using spring boot. In my UserController class i have used the below ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    After editing my UIScrollView size, page control stopped working. You can see there are only 3 slides. But ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I am new to React but I know main concept of unique key. However, I am getting a warning. Below ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Heyy mates..Am an icse student can anyone tell me any importance for java please its urgent I have my exam ... please for icse java. Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    The Cookies Preferences box that always appears in the bottom right hand corner of the dataplatform.ibm.com ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Here's the code: ''' file_path = (r'C:\Users\Luka\Desktop\Pyhton exercises\pi_digits.txt') with open( ... file_object: print(line) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Assume that I am using Tableau Desktop and have a live connection to Cloudera Hadoop data. I need to press ... the visualization every x minutes instead of pressing F5 everytime?...
asked Oct 30, 2020 in Technology by JackTerrance
...