I want to make several different module layouts
For example, I made one separate layout and named it user. Now, I have two layouts, one main, root layout and user layout.
In the main layout, I made a button that leads to the user component. And in the user layout, I made a button that leads to the main component.
The task before me is to make it so that when the button is pressed, the component from which I leave disappears. What I mean is that if I'm in the main root component and I press the "user" button which leads to the /user page, then after clicking I should go to the /user page and the main component should disappear. Or if I am in the /user component and press the "home" button that leads to the /home page, then after clicking I should go to the main /home and the /user component should disappear.
I implemented this using a service.
After I did, I noticed that if I go to the /user page and try to refresh it, then at some point, for a fraction of a second, I notice the main component. He appears and disappears. This happens very quickly and at the same time - it is very noticeable. It worries me, I don't know how to get rid of this defect. Please tell me how to get rid of this problem?
AppComponent
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnDestroy {
show: boolean = true;
subscription: Subscription;
constructor(private showContentService: ShowContentService) {
this.subscription = showContentService.contentHome.subscribe(value => {
this.show = value;
});
}
hideContent() {
this.show = false;
}
ngOnDestroy() {
// Prevent memory leak when component is destroyed
this.subscription.unsubscribe();
}
}
app.component.html
<ng-container *ngIf="show">
<p>Main page</p>
<button (click)="hideContent()" routerLink="/user">User</button>
</ng-container>
<router-outlet></router-outlet>
AppRoutingModule
const routes: Routes = [
{ path: 'user', loadChildren: () => import('./user/user.module').then(m => m.UserModule) }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
ShowContentService
@Injectable({
providedIn: 'root'
})
export class ShowContentService {
// Observable string sources
private showContent = new Subject<boolean>();
// Observable String Streams
contentHome = this.showContent.asObservable();
showContentHome(showContent: boolean) {
this.showContent.next(showContent);
}
}
UserComponent
export class UserComponent {
constructor(private showContentService: ShowContentService) {
this.showContentService.showContentHome(false);
}
showContentHome() {
this.showContentService.showContentHome(true);
}
ngOnInit(): void {
}
}
user.component.html
<p>User page</p>
<button (click)="showContentHome()" routerLink="/">Home</button>
UserRoutingModule
const routes: Routes = [
{ path: '', component: UserComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UserRoutingModule { }