Angular 更新模板中的“我的日期”对象失败

Angular 更新模板中的“我的日期”对象失败,angular,state-management,ngxs,Angular,State Management,Ngxs,实际上,我的日期对象的ngxs(3.2.0)状态管理有一个问题。我正在尝试更新我的应用程序中的显示日期(使用Angular材质的Angular 6应用程序),操作已正确调度,并且状态中的date对象已更改,但当日期更改时,订阅者不会执行任何操作,因此我的模板不会更新 这是我的操作文件currentDate.action.ts: 导出类CreateDate{ 静态只读类型=“[app]创建日期”; 构造函数(公共有效负载:日期){} } 出口类别更改日期{ 静态只读类型=“[app]更改日期”;

实际上,我的日期对象的ngxs(3.2.0)状态管理有一个问题。我正在尝试更新我的应用程序中的显示日期(使用Angular材质的Angular 6应用程序),操作已正确调度,并且状态中的date对象已更改,但当日期更改时,订阅者不会执行任何操作,因此我的模板不会更新

这是我的操作文件currentDate.action.ts:

导出类CreateDate{
静态只读类型=“[app]创建日期”;
构造函数(公共有效负载:日期){}
}
出口类别更改日期{
静态只读类型=“[app]更改日期”;
构造函数(公共有效负载:日期){}
}
导出类递增日期{
静态只读类型=“[app]增量日期”;
构造函数(){}
}
导出类递减日期{
静态只读类型=“[app]递减日期”;
构造函数(){}
}
这是我的currentDate.state.ts:

导出接口CurrentDateModel{
所选日期:日期;
当前日期:日期;
检查人:编号;
}
@陈述({
名称:“当前日期”,
默认值:{
currentDate:新日期(),
selectedDate:新日期(),
审核人:0
},
})
导出类CurrentDateState{
@操作(创建日期)
createDate(ctx:StateContext,{payload}:createDate){
patchState({currentDate:payload});
}
@操作(增量检查器)
增量检查器(ctx:StateContext){
const state=ctx.getState();
state.checker=state.checker+1;
设置状态(状态);
}
@操作(递增日期)
递增日期(ctx:StateContext){
const state=ctx.getState();
state.selectedDate.setMonth(state.selectedDate.getMonth()+1);
设置状态(状态);
}
@动作(递减)
递减日期(ctx:StateContext){
const state=ctx.getState();
state.selectedDate.setMonth(state.selectedDate.getMonth()-1);
设置状态(状态);
}
@行动(更改日期)
changeDate(ctx:StateContext,{payload}:changeDate){
patchState({currentDate:payload});
}
}

my header.ts文件中的代码:

@组件({
选择器:“应用程序标题”,
templateUrl:'./header.component.html',
样式URL:['./header.component.css']
})
导出类HeaderComponent在Init上实现{
@选择(state=>state.currentDate.selectedDate)currentDate:Observable;
@选择(state=>state.currentDate.checker)检查:可观察;
构造函数(私有存储:存储){}
恩戈尼尼特(){
this.currentDate.subscribe((date)=>console.log('hello from header',date');
this.check.subscribe(()=>console.log('i change'));
}
增量(){
this.store.dispatch(new IncrementChecker());
}
递减{
this.store.dispatch(new DecrementDate());
}
递增日期(){
this.store.dispatch(new IncrementDate());
}
}
在my template.html文件中:


测验
{{check}异步}
左箭头
{{currentDate | async | date:'MMMM'| titlecase}
向右箭头
Se连接器
定期的加薪
我尝试了很多方法(这就是为什么你也可以在我的文件中看到“check”变量),但没有成功地看到日期对象的更新,而是成功地看到了另一个对象,如我的checker here或我的others state对象

如果您有任何想法,或者您看到我的代码中有一个大错误…

在您的操作中:

@Action(IncrementDate)
IncrementDate(ctx: StateContext<CurrentDateModel>) {
    const state = ctx.getState();
    state.selectedDate.setMonth(state.selectedDate.getMonth() + 1);
    ctx.setState(state);
}
见:

或者,您可以使用patchState而不是setState:

我不确定,但问题不在于修改现有状态吗?我相信你应该在你的
IncrementDate
reducer中创建一个新的状态。是的,你是对的,正如你提到的,我必须编辑我的状态。我最终得到了ctx.patchState({selectedDate:newdate(…)});很高兴我能帮忙
let tmpDate = state.selectedDate;
ctx.setState({...state, selectedDate: tmpDate.setMonth(tmpDate.getMonth() + 1});