Давайте предположим, что у меня есть SoccerFieldComponent
, у которого есть провайдер, который назвал SoccerFieldService
SoccerFieldComponent:
@Component({
providers: [SoccerFieldService]
})
class SoccerFieldComponent extends Component {
//yada yada....
}
SoccerFieldService:
class SoccerFieldService {
//a lot of observables/functions that related to the soccer field component
}
Теперь давайте предположим, что SoccerFieldComponent может использоваться несколько раз в моей сложной системе, единственное, что будет отличаться, - это компонент, который его оборачивает, давайте дадим ему имя StadiumComponent
StadiumComponent:
@Component({
template: "some-complex-template-that-contains-stadium-field-component.html"
})
class StadiumComponent {
}
Я действительно хотел бы представить некоторые из SoccerFieldService
данных вне SoccerFieldComponent
, чтобы StadiumComponent
мог их использовать.
Мое текущее решение:
Внутри StadiumComponent
:
@ViewChild(SoccerFieldComponent, {static: false})
set soccerFieldComponent(componentRef: SoccerFieldComponent) {
if (componentRef) {
this.soccerFieldComponentRef = componentRef;
this.changeDetection.detectChanges();
}
}
Внутри SoccerFieldComponent
:
get currentScore$(): Observable<string> {
return this.soccerFieldService.currentScore$.pipe(
distinctUntilChanged(),
);
}
Теперь яМожно получить доступ через компонент к конкретным наблюдаемым внутри службы.
Есть еще идеи?