angular - 类型 'take' ngrx/store 上不存在属性 'Store<State>'

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

在我的功能中选择状态时,我尝试使用属性“take”,但出现错误 Property 'take' does not exist on type 'Store<State> .拜托,有人知道发生了什么事吗?我想要这样做的原因是因为我必须确定用户何时单击了将我的模板更改为表单的“添加空间”按钮。由于此模式还会更改父项的模板,因此我使用 @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));
      }
    }

最佳答案

尝试导入它

 import 'rxjs/add/operator/take'; 

对于 Angular > 6,Angular 应用程序包的大小使用 lettable 运算符减小了,因为它们是tree-shakable

import { take } from 'rxjs/operators';
...

this.store.select('spaces').pipe(take(1)).subscribe();

关于angular - 类型 'take' ngrx/store 上不存在属性 'Store<State>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47252892/

相关文章:

javascript - 错误 : Effect dispatched an invalid action: undefined

angular - NgRx 8 测试 provideMockStore - 状态切片的 setState

angular - ngrx/router-store - 路由参数选择器为子路由返回 undefined

apache - 当 ng build dist 文件夹(重命名为 myapp)部署在 tomcat webapp 文件夹中时,Angular 2 无法加载图像

javascript - 使用 JsonPipe 纠正 Angular 中的缩进 json

angular - ng serve 无法在 EC2 服务器上运行

angular - 如何在 Angular 2+ 中使用 HostListener 查找浏览器选项卡是否集中

angular - 如何将 NgRx 8 操作与 NgRx 7 reducer 一起使用

Angular ng rx store从map()获取保存的对象并在Effect内部的tap()中使用它

javascript - NgRX store.select 返回存储对象而不是可观察对象