更新
从@NGXS v3.1 开始,他们终于在@Selector() 中引入了参数。
https://www.ngxs.io/concepts/select#lazy-selectors
DOCS 中的示例
首先,您定义 @Selector “ pandas ”
@State<string[]>({
name: 'animals',
defaults: []
})
@Injectable()
export class ZooState {
@Selector()
static pandas(state: string[]) {
return (type: string) => {
return state.filter(s => s.indexOf('panda') > -1).filter(s => s.indexOf(type) > -1);
};
}
}
然后你只需在你的'.ts'文件中调用它
import { Store } from '@ngxs/store';
import { map } from 'rxjs/operators';
@Component({ ... })
export class ZooComponent {
babyPandas$: Observable<string[]>;
constructor(private store: Store) {
this.babyPandas$ = this.store
.select(ZooState.pandas)
.pipe(map(filterFn => filterFn('baby')));
}
}
*来自旧邮报*
我正在尝试创建一个自定义 @Select () 以便能够向下钻取特定树并动态返回值。获得未定义或未定义(执行)
用户.component.ts
const location = 'new york'
@Select(state => UserState.getUserLocationSlots(state, location)) slots$;
用户状态.ts
@Selector()
static getUserLocationSlots(state: UserStateModel, location: any) {
console.log(state);
console.log(location); // <-- expecting 'new york', but getting undefined
}