物业&#39;采取&#39;类型&#39;存储<state>&#39; NGRX /存储

时间:2017-11-12 19:20:34

标签: angular ngrx ngrx-store angular5 ngrx-store-4.0

我试图使用该物业&#39;采取&#39;在我的功能中选择状态但我收到错误Property 'take' does not exist on type 'Store<State>时。请问,任何人都知道发生了什么事吗?我想这样做的原因是因为我必须确定用户何时点击按钮&#34;添加空格&#34;这会将我的模板更改为表单。由于此模式也会更改父模板,因此我使用@ ngrx / store管理它。

我的代码:

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));
      }
    }

3 个答案:

答案 0 :(得分:27)

尝试导入

 import 'rxjs/add/operator/take'; 

表示角度6

import { take } from 'rxjs/operators';

答案 1 :(得分:16)

如果是角度6:

1)从rxjs导入

import { take } from 'rxjs/operators';

2)与pipe方法一起使用

.pipe(take(1))

答案 2 :(得分:2)

是最新版本的angular的兼容性问题,您需要rxjs-compat。

解决方案:

npm install --save rxjs-compat