I'm trying to use the property 'take' when selecting a state in my feature but I'm getting the error Property 'take' does not exist on type 'Store<State>
. Please, anyone has a clue of what is going on? The reason I want to do that is because I have to identify when the user clicked on the button "Add space" which changes my template to a form. As this mode also changes the parent's template, I'm managing it with @ngrx/store.
My code:
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Store } from '@ngrx/store';
import { Subscription } from 'rxjs/Subscription';
import { Space } from '../../../shared/space.model';
// import { SpacesService } from '../../spaces.service';
import * as SpacesActions from '../../store/spaces.actions';
import * as fromSpaces from '../../store/spaces.reducers';
@Component({
selector: 'app-space-item',
templateUrl: './space-item.component.html',
styleUrls: ['./space-item.component.scss']
})
export class SpaceItemComponent implements OnInit, OnDestroy {
@Input() space: Space;
@Input() index: number;
private addMode: boolean;
private editMode = false;
// updatedSpaceName: string;
// updatedSpace: Space;
private subscription: Subscription;
updatedSpaceName: string;
updatedPicture: string;
constructor(
// private spacesService: SpacesService,
private store: Store<fromSpaces.FeatureState>) { }
ngOnInit() {
this.subscription = this.store.select('spaces')
.take(1)
.subscribe(
data => {
if (data.addMode) {
this.addMode = data.addMode;
} else {
this.addMode = false;
}
}
);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
onEnableEdit() {
this.editMode = true;
this.updatedSpaceName = this.space.name;
this.updatedPicture = this.space.picture;
// console.log(this.updatedSpace);
}
onUpdate() {
this.onCancelEdit();
const updatedSpace = new Space(this.updatedSpaceName, this.updatedPicture);
// this.spacesService.updateSpace(updatedSpace, this.index);
this.store.dispatch(new SpacesActions.UpdateSpace({index: this.index, updatedSpace}));
}
onCancelEdit() {
this.editMode = false;
// this.updatedSpace = this.space;
}
onCancelAdd() {
// this.spacesService.addModeActivated.next(false);
this.store.dispatch(new SpacesActions.SwitchAddMode(false));
}
onDelete() {
// this.spacesService.deleteSpace(this.index);
this.store.dispatch(new SpacesActions.DeleteSpace(this.index));
}
onAddSpace(form: NgForm) {
const value = form.value;
const newSpace: Space = new Space(value.spaceName);
// this.spacesService.addSpace(newSpace);
this.store.dispatch(new SpacesActions.AddSpace(newSpace));
}
}
try to import it
import 'rxjs/add/operator/take';
for Angular > 6, Angular app bundle size are reduced using lettable operators, because they are tree-shakable
import { take } from 'rxjs/operators';
...
this.store.select('spaces').pipe(take(1)).subscribe();
In case of angular 6:
1) import from rxjs
import { take } from 'rxjs/operators';
2) use it with pipe
method
.pipe(take(1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With