欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Ngrx 6.1.0-不建议使用Select-新语法是什么?

Ngrx 6.1.0-不建议使用Select-新语法是什么?

问问题 活动时间 10个月前已浏览 9k次19

不建议使用以下ngrx select。

this.store.select(state => state.academy.academy).subscribe((academy) => {
    this.academy = academy;
});

我在store.d.ts找到了

@deprecated from 6.1.0. Use the pipeable `select` operator instead.

那么...正确的语法是什么?

我尝试

this.store.pipe(select(state => state.academy.academy).subscribe((academy) => {
    this.academy = academy;
}))

错误:找不到名称“选择”。您的意思是“ onselect”吗?

改善这个问题18年8月3日在12:27 问米哈利斯2,7451金徽章32银徽章52枚青铜徽章添加评论

3个答案


import {Component, OnInit} from '@angular/core';
import {Store, select} from '@ngrx/store';
import {AppState} from '../../../../../app.state';

@Component({
   selector: 'app-layout',
   templateUrl: './layout.component.html',
   styleUrls: ['./layout.component.scss']
})
export class PageLayoutComponent implements OnInit {

   academy;

 constructor(
 private store: Store<AppState>
 ) {
 }

   ngOnInit() {
 this.store.pipe(select((state: any) => state.academy.academy)).subscribe((academy) => {
 this.academy = academy;
 });
 }


}

展开摘要


改善这个答案18年8月3日在12:40 回答米哈利斯2,7451金徽章32银徽章52枚青铜徽章添加评论11

根据NgRx 7,select不建议使用方法。

有关更多信息,请参见相关的请求请求

改善这个答案18年12月25日在10:40 回答timdeschryver5,0521金徽章4银徽章14枚青铜徽章添加评论3

正如@Michalis所提到的,只需select获得@ngrx/store

选择器使您能够为应用程序状态编写读取模型。就CQRS架构模式而言,NgRx将读取模型(选择器)与写入模型(缩减器)分开。一种高级技术是将选择器与RxJS可管道运算符结合在一起。    

此功能是在v5.0.0中添加的,此后this.store.select()不推荐使用。但是,在版本v6.1.0中添加了相同的注意事项。随着Store<T>自身的扩展Observable<T>,它返回observable,它可以使用轻松地进行预订.subscribe()或可以使用不同的补丁运算符进行操作/转换。

RxJS .pipe()v5.5中引入了pipable运算符还有一个管道实用程序功能,可用于构建可重用的可管道运算符。在版本v5中,借助pipe()自定义select运算符来构建。查看此链接或下面给出的基本示例(忽略空状态),以了解更多信息。

import { select } from '@ngrx/store';
import { pipe } from 'rxjs';
import { filter } from 'rxjs/operators';

export const selectFilteredState = pipe(
  select('sliceOfState'),
  filter(state => state !== undefined)
);

store.pipe(selectFilteredState ).subscribe(/* .. */);

改善这个答案18年9月6日在10:34 回答哈迪克·皮特瓦1,0971金徽章10银徽章26枚青铜徽章添加评论

来自  https://stackoverflow.com/questions/51672633/ngrx-6-1-0-select-is-deprecated-what-is-the-new-syntax    


Ngrx 6.1.0 - Select is deprecated - What is the new syntax?

Ask QuestionAsked Active 10 months agoViewed 9k times19

The following ngrx select is deprecated.

this.store.select(state => state.academy.academy).subscribe((academy) => {
    this.academy = academy;
});

I found this at store.d.ts

@deprecated from 6.1.0. Use the pipeable `select` operator instead.

So... what's the correct syntax?

I try

this.store.pipe(select(state => state.academy.academy).subscribe((academy) => {
    this.academy = academy;
}))

Error: Cannot find name 'select'. Did you mean 'onselect'?

improve this questionasked Aug 3 '18 at 12:27Michalis2,7451 gold badge32 silver badges52 bronze badgesadd a comment

3 Answers



import {Component, OnInit} from '@angular/core';
import {Store, select} from '@ngrx/store';
import {AppState} from '../../../../../app.state';

@Component({
   selector: 'app-layout',
   templateUrl: './layout.component.html',
   styleUrls: ['./layout.component.scss']
})
export class PageLayoutComponent implements OnInit {

   academy;

 constructor(
 private store: Store<AppState>
 ) {
 }

   ngOnInit() {
 this.store.pipe(select((state: any) => state.academy.academy)).subscribe((academy) => {
 this.academy = academy;
 });
 }


}

Expand snippet


improve this answeranswered Aug 3 '18 at 12:40Michalis2,7451 gold badge32 silver badges52 bronze badgesadd a comment11

As per NgRx 7, the select method is un-deprecated.

For more info, see the associated Pull Request.

improve this answeranswered Dec 25 '18 at 10:40timdeschryver5,0521 gold badge4 silver badges14 bronze badgesadd a comment3

As @Michalis mentioned, just get select from @ngrx/store.

Selectors empower you to compose a read model for your application state. In terms of the CQRS architectural pattern, NgRx separates the read model (selectors) from the write model (reducers). An advanced technique is to combine selectors with RxJS pipeable operators.

This feature was added in v5.0.0, and since then this.store.select() has been deprecated. However, notice for the same is added in release v6.1.0. As Store<T> itself extends Observable<T>, it returns observable which can easily be subscribed using .subscribe() or can be manipulated/transformed using different patch operators.

RxJS introduced pipable operators and .pipe() in v5.5. There is also a pipe utility function that can be used to build reusable pipeable operators. In release v5, with the help of pipe() custom select operator is built. Check out this link or basic example(ignore empty state) is given beneath, to learn more.

import { select } from '@ngrx/store';
import { pipe } from 'rxjs';
import { filter } from 'rxjs/operators';

export const selectFilteredState = pipe(
  select('sliceOfState'),
  filter(state => state !== undefined)
);

store.pipe(selectFilteredState ).subscribe(/* .. */);

improve this answeranswered Sep 6 '18 at 10:34Hardik Pithva1,0971 gold badge10 silver badges26 bronze badgesadd a comment

来自  https://stackoverflow.com/questions/51672633/ngrx-6-1-0-select-is-deprecated-what-is-the-new-syntax    


普通分类: