我試圖設置我的路由器配置使用Resolve從行爲主題返回Observable。我已經在這兩個角度4.0.0-beta8和角2.4.8 +路由器試過這種3.4.8Angular 2路由器在Resolve中使用BehaviorSubject Observable
這裏是我的服務:
@Injectable()
export class MyService {
private _data: BehaviorSubject<Array<string>> = new BehaviorSubject(undefined);
constructor() {}
public getData(): Observable<Array<string>> {
this._data.next(['test1', 'test2', 'test3']);
let asObservable = this._data.asObservable().delay(1000);
asObservable.subscribe((myData) => {
console.log([myData, 'this console message DOES show up']);
});
// if I return here, my component's constructor and ngOnInit never fire
// return asObservable;
let fakeObservable = Observable.of(['test1', 'test2', 'test3']).delay(1000);
fakeObservable.subscribe((fakeData) => {
console.log([fakeData, 'this console message shows up']);
});
console.log([asObservable, fakeObservable]);
/* console log output
Observable {
_isScalar: false,
operator: DelayOperator,
source: Observable {
_isScalar: false,
source: BehaviorSubject {
_isScalar: false,
_value: ['test1', 'test2', 'test3'],
closed: false,
hasError: false,
isStopped: false,
observers: Array[1],
thrownError: null,
value: ['test1', 'test2', 'test3']
}
}
},
Observable {
_isScalar: false,
operator: DelayOperator,
source: ScalarObservable {
_isScalar: true,
scheduler: null,
value: ['test1', 'test2', 'test3']
}
}
*/
return fakeObservable; // this WILL reach my component constructor and ngOnInit
}
}
這裏是我的決心
@Injectable()
export class MyResolver implements Resolve<Array<string>> {
constructor(private myService: MyService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Array<string>>|undefined {
return this.myService.getData();
}
}
這裏的路由器
RouterModule.forChild([{
path: 'mypath',
component: MyComponent,
resolve: {
data: MyResolver
}
}]);
而這裏的組件:
@Component({
selector: 'my-component',
template: '<Span>My Component</span>'
})
export class MyComponent implements OnInit {
constructor(private route: ActivatedRoute) {
console.log('component constructor');
}
ngOnInit(): void {
console.log(this.route.snapshot.data['data']); // ['test1', 'test2', 'test3']
}
}
這可能不是設計解決方案和服務之間交互的最佳方式,所以我非常樂於接受建議。但是,如果我不明白爲什麼BehaviorSubject.asObservable()不起作用,但是模擬的observable確實有效,我可能會發瘋。
什麼問題? 「不起作用」是沒有信息的。 –