Angular为什么subscribe订阅不到值?
app.component.ts
import { Component } from '@angular/core';
import { AppService } from './service/app.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private service : AppService){
this.service.appStore.subscribe(state => {
console.log(state);
})
}
ngOnInit(){
this.service.getData();
}
testClick(){
console.log("111")
}
}
app.service.ts
import { Data } from "../mock/data";
import { Person } from "../models/person";
import { Observable } from "rxjs/Observable";
import { Store } from "@ngrx/store";
import { AppState, createAppState } from "../app.state";
import { AppAction } from "../actions/app.action";
import { Injectable } from "@angular/core";
@Injectable()
export class AppService{
appStore: Store<AppState>;
constructor(private store : Store<any>){
this.appStore = this.store.select(createAppState);
}
getData() {
this.store.dispatch(new AppAction({
data : Data
}));
}
}
app.state.ts
import { createFeatureSelector, createSelector } from "@ngrx/store";
import { Person } from "./models/person";
export class AppState {
entity : Array<Person> = [];
}
export const getAppState = createFeatureSelector<AppState>('app');
export const createAppState = createSelector(
getAppState,
(state: AppState) => new AppState()
)
app.reducer.ts
import { AppState } from "../app.state";
import { AppAction } from "../actions/app.action";
export function appReducer(
state = new AppState(),
action : AppAction
) {
switch(action.type){
case "appAction" : {
let s = new AppState();
console.log(action.payload.data)
s.entity = action.payload.data;
return s;
}
default: {
return new AppState();
}
}
}
app.action.ts
import { Action } from "@ngrx/store";
import { Person } from "../models/person";
export class AppAction implements Action{
type = "appAction";
constructor(
public payload :{
data : Array<Person>
}
){
}
}
为什么订阅不了action发过来的值。entity居然是空数组,reducer的switch明明可以打印出值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
app.state.ts 中的 createAppState 一直在返回一个新的new出来的对象。