简体   繁体   English

Angular + Redux + NGRX:获取数据

[英]Angular + redux + ngrx: get data

I'm writting my first Angular 2/4 application with redux (I'm new to this pattern), and I'm stuck in a problem. 我正在用redux编写我的第一个Angular 2/4应用程序(我是这种模式的新手),但我陷入了一个问题。 I~m using @ngrx/store 我〜使用@ ngrx / store

There is a model, called Content. 有一个名为Content的模型。 My store (app.store.ts) is: 我的商店(app.store.ts)是:

export interface AppStore {
    contents: {id: number, Content};
    contentContents: {id: number, Array};
}

'contents' has a list of all contents (id 1, content 1; id 2, content 2). “内容”具有所有内容的列表(ID 1,内容1; ID 2,内容2)。 'contentContents' has a list of contents and its child. “ contentContents”具有内容及其子级的列表。 A content can have child contents. 内容可以具有子内容。 So I want to keep an object like: 所以我想保留一个像这样的对象:

{ 1: [2, 3, 6], 2: [7, 9, 10], 6: [11, 4, 5] } {1:[2,3,6],2:[7,9,10],6:[11,4,5]}

My reducers are doing this already, but I have to problems. 我的减速器已经在做这个,但是我必须解决问题。

I have selected the contentContents: 我选择了contentContents:

let subscription = this.store.select( store => {
    return store.contentContents;
})

and subscribed to it: 并订阅了:

subscription.subscribe( contentContents => {

})

is there a way to subscribe to a particular item from contentContents? 有没有办法从contentContents订阅特定项目? I mean, I want to know when a particular item from contentContents is changed. 我的意思是,我想知道contentContents中的特定项目何时更改。 Example: the component what is displaying content #1 has to be warned only when contentContents[1] is changed, but in the way I have built it is being warned everytime the contentContents is changed. 示例:仅当contentContents [1]更改时才需要警告正在显示内容#1的组件,但是按照我构建的方式,每次contentContents更改时都会对其进行警告。

Other thing, if I can resolve the first problem: is there a way to query from the store the content from a particular id? 另一件事,如果我可以解决第一个问题:是否可以从存储中查询特定ID的内容?

Let's say that I have this: 1: [2, 3, 6] Content #1 has contents #2, #3 and #6 as child. 假设我有以下内容:1:[2,3,6]内容#1作为子对象具有内容#2,#3和#6。 How to get content #2, content #3 and content #6 如何获得内容2,内容3和内容6

try this. 尝试这个。

subscription.map(contentContents => 
contentContents.filter(contentContent.id === reqContent.id));

the subscription 订阅

subscription.subscribe( contentContent => {
  console.log('contentContent', contentContent);
})

Already a while ago that you asked this but maybe still usefull: 不久前您曾问过这个问题,但也许仍然有用:

NGRX has memoized selectors which are automatically created when you use the select function or which can be created explicitly: https://github.com/ngrx/platform/blob/master/docs/store/selectors.md NGRX具有备忘的选择器,这些选择器会在您使用选择功能时自动创建,也可以显式创建: https : //github.com/ngrx/platform/blob/master/docs/store/selectors.md

So to get notified about a change of one specific item I would write a function so select that item. 因此,要获得有关某个特定项目更改的通知,我将编写一个函数,然后选择该项目。 Something like function getContentById(id:number,contentContents){ return contentContents.filter(contentContent.id === id)); } 类似于function getContentById(id:number,contentContents){ return contentContents.filter(contentContent.id === id)); } function getContentById(id:number,contentContents){ return contentContents.filter(contentContent.id === id)); }

Then create an observable which utilizes this function: 然后创建一个利用此功能的可观察对象:

content1$ = this.store.select(s => getContentById(s.contentContents,1))

and subscribe to this in the userinterface with an async pipe instead of an explicit subscribe in your component <div>{{content1$|async}}</div> In this way you don't need to unsubscribe from the subscription manually https://angular.io/api/common/AsyncPipe 并使用异步管道在用户界面中进行订阅,而不是在组件<div>{{content1$|async}}</div>中进行显式订阅,这样,您无需手动取消订阅https: //angular.io/api/common/AsyncPipe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM