모든 사용자 정보가 저장되고 userService에서 제공되는 관측 가능 정보로 업데이트되는 사용자 개체가있는 응용 프로그램이 있습니다. 이제는 컴포넌트로 관찰 할 수있는 사용자를 사용하는 것이 가장 좋은 방법인지 궁금합니다.2 각 구성 요소 모범 사례
캘린더 구성 요소 (캘린더)가있는 페이지 (TestPage)가 있습니다. 캘린더는 주간보기 또는 월별보기가 표시되어 있어야하므로 사용자 객체를 사용합니다.
옵션 1 : 첫 번째 옵션은 TestPage에서 관찰 하나를 만드는 것입니다 및 속성과 함께 사용자 개체를 제공하고 달력, 그것은 @input
와 사용자를 가져옵니다.
TestPage의 타이프 라이터 파일 :
export class TestPage {
private user: User;
private userSubscription: Subscription;
constructor(private usersService: UsersService) {
this.log('constructor');
this.userSubscription = this.usersService.$currentUser.subscribe((user: User) => {
this.log('$currentUser: user update', user);
this.user = user;
});
}
ngOnDestroy(): void {
this.log('ngOnDestroy: unsubscribing userSubscription');
this.userSubscription.unsubscribe();
}
}
TestPage의 HTML :
<flex-calendar [user]="user"></flex-calendar>
일정 :
export class Calendar {
@Input() user: User; // Got the user from the TestPage
}
옵션 2 : 는 산부인과 만들기 TestPage 및 달력에서 제공 가능합니다. 이 옵션은 캘린더가 페이지와의 연결이 적다는 이점이 있습니다.
일정 :
export class Calendar {
private user: User;
private userSubscription: Subscription;
constructor(private usersService: UsersService) {
this.log('constructor');
this.userSubscription = this.usersService.$currentUser.subscribe((user: User) => {
this.log('$currentUser: user update', user);
this.user = user;
});
}
ngOnDestroy(): void {
this.log('ngOnDestroy: unsubscribing userSubscription');
this.userSubscription.unsubscribe();
}
}
누군가가 최선의 선택이 될 왜 것이 무엇 설명 할 수 있습니까?
코드를 게시하십시오. 나는 그 시도가 정확하게 보이기로되어있는 것을 얻지 못한다. –
나는 지금 약간의 코드를 추가했다 : 나는 이해하기가 더 어려울 것이기 때문에 모든 것을 추가 할 수 없었다. – mbakker1996
나는 문제가 무엇인지 모르겠다. 두 코드 블록은 클래스 이름을 제외하고는 동일하게 보입니다. –