2017/10/26
Meguro.es #12
背景
Angular
UIコンポーネントの分離+デモアプリ
<!-- user$ は Observable<User> もしくは Promise<User> -->
<ng-container *ngIf="user$ | async as user; else loading">
<span>{{ user.name }}</span>
<span>{{ user.mail }}</span>
</ng-container>
<ng-template #loading>
<my-spinner></my-spinner>
</ng-template>
export interface Todo {
id: number;
text: string;
done: boolean;
}
@Injectable()
export class TodoService {
_todos = new BehaviorSubject<Todo[]>([
{ id: 1, text: 'learn angular', done: false },
{ id: 2, text: 'learn react', done: true },
]);
todos = this._todos.asObservable();
constructor() { }
fetch(): Observable<Todo[]> {
return this.todos;
}
addTodo(text: string) {
const todos = this._todos.getValue();
const nextId = todos.length + 1;
this._todos.next([ ...todos, { id: nextId, text, done: false }]);
}
}
Redux | Angular + RxJS | |
---|---|---|
Store | JSON | 複数のSubject |
Storeの結合 | combineReducers | (combineLatest とか) |
selector | あり | (ServiceのDIとか) |
Action | ActionCreatorの実行 | Serviceメソッドの実行 |
状態の変化 | reducer | Subject#next |
{ ... }
Redux
RxJS
Componentのasync pipeにObservable突っ込むだけ、相性抜群
map/filter/reduce/flatMap/combineLatestなどでObservableを加工する
1枚のJSONがAction/Reducerで更新されていく