没有
connect
在这里。当你处理角度时,你主要处理来自rxjs的观测数据。
@angular-redux/store
图书馆使用
Select Pattern
因为它能很有效地插入到角度变化检测机制中。
它给了我们两个选择:
@select装饰符
// this selects `counter` from the store and attaches it to this property
// it uses the property name to select, and ignores the $ from it
@select() counter$;
// this selects `counter` from the store and attaches it to this property
@select() counter;
// this selects `counter` from the store and attaches it to this property
@select('counter') counterSelectedWithString;
// this selects `pathDemo.foo.bar` from the store and attaches it to this
// property.
@select(['pathDemo', 'foo', 'bar']) pathSelection;
// this selects `counter` from the store and attaches it to this property
@select(state => state.counter) counterSelectedWithFunction;
// this selects `counter` from the store and multiples it by two
@select(state => state.counter * 2)
counterSelectedWithFuntionAndMultipliedByTwo: Observable<any>;
在构造函数中注入ngredux实例
(多亏了
Angular DI
)以下内容:
import * as CounterActions from '../actions/CounterActions';
import { NgRedux } from '@angular-redux/store';
@Component({
selector: 'root',
template: `
<counter [counter]="counter$| async"
[increment]="increment"
[decrement]="decrement">
</counter>
`
})
export class Counter {
private count$: Observable<number>;
constructor(private ngRedux: NgRedux<IAppState>) {}
ngOnInit() {
let {increment, decrement } = CounterActions;
this.counter$ = this.ngRedux.select('counter');
}
incrementIfOdd = () => this.ngRedux.dispatch(
<any>CounterActions.incrementIfOdd());
incrementAsync = () => this.ngRedux.dispatch(
<any>CounterActions.incrementAsync());
}
你可以把这个模式看作是
reselect
对于RXJS重角度世界。
完整示例请参见
example-app
或
simple counter example