如何在 ngrx 中使用组件存储和组合?

问题描述 投票:0回答:1

在 ngrx component-store 文档中 他们在示例中使用了继承:

export interface MoviesState {
  movies: Movie[];
}

@Injectable()
export class MoviesStore extends ComponentStore<MoviesState> {
  
  constructor() {
    super({movies: []});
  }
}

-- OR --

@Component({
  template: `
    <li *ngFor="let movie of (movies$ | async)">
      {{ movie.name }}
    </li>
  `,
  providers: [ComponentStore],
})
export class MoviesPageComponent {
  readonly movies$ = this.componentStore.select(state => state.movies);
 
  constructor(
    private readonly componentStore: ComponentStore<{movies: Movie[]}>
  ) {}
 
  ngOnInit() {
    this.componentStore.setState({movies: []});
  }
}

我想知道是否可以使用组件存储进行组合? (我的意思是不使用

extends
关键字来继承)如果是这样,它看起来如何?

angular ngrx
1个回答
0
投票

如果你想保留组件存储,那么是的,这是唯一的方法。

扩展添加了这个方法https://next.ngrx.io/api/component-store/ComponentStore.

© www.soinside.com 2019 - 2024. All rights reserved.