我需要一张地图进入我的服务在角6应用程序。目前,当我在服务中调用init函数时,我通过参数获得它,并在从存储中获取数据之后,通过组件通过Subject提供它。但有时我会得到undefined而不是地图。
我的代码示例:
page.component.ts
export class PageComponent implements OnInit {
map$ = new Subject<Map>();
ngOnInit() {
this.store.pipe(
select(fromStore.getData)
)
.subscribe(data => {
this.mapService.doSomething(this.map$);
})
}
mapReady(map: Map) {
this.map$.next(map);
}
}map.service.ts
export class MapService {
map: Map;
subsription$: Subscription;
doSomething(map$: Observable<Map>) {
this.subsription$ = map$.subscribe(data => {
this.map = data;
})
}
}有什么其他选择可以让地图进入服务吗?
发布于 2019-02-26 16:01:12
你为什么不能更直接地访问地图呢?似乎你有一些额外的间接方向,你可能不需要。就像这样:
map.service.ts:
export class MapService {
map: Map = new Map();
doSomething(map: Map) {
this.map = map;
}
}page.component.ts:
export class PageComponent implements OnInit {
ngOnInit() {
this.store.pipe(
select(fromStore.getData)
)
.subscribe(data => {
this.mapService.doSomething(data);
})
}
mapReady(map: Map) {
this.mapService.doSomething(map);
}
}确切的类型可能不太正确,因为我不知道this.store和fromStore在做什么,但是它应该很容易与实际工作的东西相适应。
https://stackoverflow.com/questions/54888600
复制相似问题