angular - Angular路由器在检索状态对象时如何将window.history.state转换为一个类
问题描述
我正在使用以下代码示例将对象传递给使用状态的路由
@Component({
template: `<a (click)="navigateWithState()">Go</a>`,
})
export class AppComponent {
constructor(public router: Router) {}
navigateWithState() {
this.router.navigateByUrl('/123', { state: { hello: 'world' } });
}
}
在阅读状态时,我想将其转换为类对象。如何将其转换为类对象?
@Component({
selector: 'app-details-page',
template: '<pre>{{ state$ | async | json }}</pre>'
})
export class DetailsPageComponent implements OnInit {
state$: Observable<MyClass>;
constructor(public activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.state$ = this.activatedRoute.paramMap
.pipe(map(() => (window.history.state) as MyClass))
}
}
谢谢
解决方案
推荐阅读
- c++ - 如何获得带有引号的句子?
- git - 弹出并重放一堆 git 提交
- vba - 根据货币乘以每个单元格
- karma-jasmine - 尽管在 fakeAsync 中刷新,但计时器仍在队列中
- sql - 有没有办法在 Oracle 19 中向 JSON 数组添加/附加值?
- c++ - 数学函数的不一致死码消除
- javascript - 如何将流从客户端上传并保存到 mp4 中的节点服务器
- reactjs - 即使在卸载组件后,useEffect 中的此清理功能也会运行吗?
- reactjs - 反应:未处理的拒绝(TypeError):无法读取未定义的属性'catch'
- wireshark - ip.src!=ADDR 和 !ip.src==ADDR 的区别