我正试图使用解析器来根据路由持有的给定参数检索数据。
不幸的是,当我添加另一个数据流时,我的数据依赖于解析器,但实际上从未解析。
如果我直接返回一个立即解决的值,一切正常。
我调试了情况,看到我收到了所有的部分信息,但最终却没有真正解决。
这是一个快速样品。如果需要更多的代码来理解这个问题,请给我打电话。
我的服务:
export class MyService {
get(bar) {
return of(new Foo(bar));
}
}
secondservice(此服务从后端检索数据):
export class SecondService {
private readonly _observable: Observable<Bar>;
constructor() {
this._observable = merge(
// Other manipulations
).pipe(
// other manipulations
shareReplay(1)
)
}
observable(): Observable<Bar> {
return this._observable;
}
}
分解器:
export class MyResolver {
constructor(_secondService: SecondService, _myService: MyService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
// Does not work - Simply does not resolve
// return this._secondService
// .observable()
// .pipe(
// switchMap((bar) => this._myService.get(bar)),
// );
// WORKS
return of(new Foobar('sampleData'));
}
}
路由器:
const routes: Routes = [
{
path: 'someroute',
component: SomeComponent,
canActivate: [SomeGuard],
resolve: {
myData: MyResolver,
},
},
];
组件:
export class SomeComponent implements OnInit {
constructor(private readonly _route: ActivatedRoute) {}
ngOnInit() {
this._route.data
.subscribe((data) => {
console.log('received:', data);
this.myData = data;
});
}
}
somecomponent.html文件
<pre *ngIf="myData">
Received: {{ myData | json }}
</pre>