我有一个包含 JSON 的状态,如下所示:
{id: "1", name: "ig1", description: "ig 11"}
{id: "5", name: "hhh", description: "hhh"}
{id: "6", name: "ggg", description: "hhh"}
我需要从状态数组中选择 id = 1 的数据
如何使用 NGXS 状态选择器来做到这一点
我们如何在组件中访问它?
我的状态有以下内容
食谱
0 : {id: "3", name: "Cake", image: "index.jpg", description: "Lorem Ipsum is
simply dummy text of the printing a…ldus PageMaker including versions of
Lorem Ipsum.", ingredients: "ig 1-1kg,ig 123-4kg"}
1 : {id: "16", name: "gfdg", image: "Chrysanthemum.jpg", description:
"fhfgh",
ingredients: "gdfg-4sfhs,ig 1-1kg"}
2 : {id: "17", name: "hfgh", image: "Jellyfish.jpg", description: "ghgfh",
ingredients: "ig 123-5kg,ig 1-3kg"}
3 :{id: "18", name: "hgj", image: "Koala.jpg", description: "ghjhgj",
ingredients: "gdfg-5sfhs"}
我想获取索引1的状态数据,即{id:“16”,名称:“gfdg”,图像:“Chrysanthemum.jpg”,描述:“fhfgh”,成分:“gdfg-4sfhs,ig 1-1kg "}
根据您的回复,我添加了以下代码
在组件中:
import { RecipeState } from '../../state/recipe.state';
edit_details : Observable<string[]>;
editRecipepopup( id ) {
this.edit_details = this.store.select(RecipeState.findById).pipe(map(filterFn
=> filterFn(id)));
console.log("edit=>",this.edit_details);
}
在状态:
@Selector()
static findById(state: string[]) {
return (id: string) => { //<--- Return a function from select
console.log("id=>"+id);
return state.filter(s => s.indexOf(id) > -1);
};
}
但我没有得到数据。我的身份证也没有登录控制台