How can I call the toggleSidebar() method of the SidebarComponent from the HeaderComponent using the callToggleSidebarOnToggleSidebarBtn() method? I am encountering an error that I cannot comprehend. What is the correct way to use a method of one component in another?
I have imported the SidebarComponent into the HeaderComponent. In the constructor of HeaderComponent, I added the SidebarComponent to use one of its methods. Even after removing the callToggleSidebarOnToggleSidebarBtn() method from header.component.ts, I still face the same issue.
sidebar.component.html :
<div id="sidebar" class="bg-gray">
<a href="" class="nav-link"><i class="fa fa-calendar"></i> Event</a>
<a href="" class="nav-link"><i class="fa fa-user-circle-o"></i> Profile</a>
<a href="" class="nav-link"><i class="fa fa-commenting-o"></i> Chat</a>
<a href="" class="nav-link"><i class="fa fa-gear"></i> Setting</a>
<a href="" class="nav-link"><i class="fa fa-question-circle-o"></i> Help</a>
<a href="" class="nav-link"><i class="fa fa-sign-out"></i> Log out</a>
header.component.html :
<div>
<nav class="navbar navbar-expand-lg d-flex justify-content-between fixed-top">
<button class="btn" (click)="callToggleSidebarOnToggleSidebarBtn()"><i class="fa fa-reorder"></i></button>
<div class="d-flex p-0 m-0">
<a href="#" class="nav-link d-none d-lg-block">Event</a>
<a href="#" class="nav-link d-none d-lg-block">Contact Us</a>
<a href="#" class="nav-link d-none d-lg-block">About Us</a>
<a href="#" class="nav-link d-none d-lg-block"><i class="fa fa-user-circle"></i></a>
</div>
<div class="d-flex">
<a href="#" class="nav-link"><i class="fa fa-bell"></i></a>
<a href="#" class="nav-link"><i class="fa fa-globe"></i></a>
</div>
</nav>
<div class="d-sm-flex align-items-center justify-content-between" id="secondMenu">
<div id="logoContainer">
<img src="../assets/images/logo.png" alt="" id="logo">
</div>
<div class="input-group">
<div class="input-group-prepend">
<button class="input-group-text btn dropdown-toggle" data-toggle="dropdown">Meeting</button>
<div class="dropdown-menu">
<a class="dropdown-item" class="dropdown-item">Action</a>
<a class="dropdown-item" class="dropdown-item">Another action</a>
<a class="dropdown-item" class="dropdown-item">Something else here</a>
</div>
</div>
<input type="text" class="form-control" placeholder="What are you looking for ?" aria-label="Search">
<div class="input-group-append">
<button class="btn" type="submit"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
<div>
I expected to be able to toggle the content of sidebar.component.html by clicking on the button at the top left of the header. However, I encountered the following error:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)
[HeaderComponent -> SidebarComponent]:
StaticInjectorError(Platform: core)[HeaderComponent -> SidebarComponent]:
NullInjectorError: No provider for SidebarComponent!
Error: StaticInjectorError(AppModule)[HeaderComponent -> SidebarComponent]:
StaticInjectorError(Platform: core)[HeaderComponent -> SidebarComponent]:
NullInjectorError: No provider for SidebarComponent!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:3228)
at resolveToken (core.js:3473)
at tryResolveToken (core.js:3417)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:3314)
at resolveToken (core.js:3473)
at tryResolveToken (core.js:3417)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:3314)
at resolveNgModuleDep (core.js:19784)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:20473)
at resolveDep (core.js:20844)
at resolvePromise (zone.js:831)
at resolvePromise (zone.js:788)
at zone.js:892
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:16147)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)