Search code examples
angularangular-componentsangular-router

Angular 2 sidebar component not working on route navigate


Sidebar component is not responding properly when I navigate Internally

When I run onSubmit from my login.component.ts it goes to dashboard/home, 'home' view works fine and My sidebar component loads too but when I click sidebar-items, routerLink doesn't work, and neither the dropdown.

On my login,component.ts I have made a simple authenticate, which when successful will go to the dashboard route which has

onSubmit(values){
    this.http.post('/user/authenticate', loginObj)
      .map((res) => res.json())
      .subscribe((res)=> {
        if(res.success == true){
          this.router.navigate(['/dashboard']);
        } else {
          ....
        }
}

and in my dashboard.component.html

<div id="wrapper" >
  <app-sidebar ></app-sidebar>
      <div id="page-wrapper" class="gray-bg">
         <app-header></app-header>
      <router-outlet></router-outlet>
     </div>
</div>

But the main problem is that when i navigate to this route, my Sidebar Component doesn't render and links don't work. I have tried even ngZone on my login submission

this.zone.run(() => {
  this.router.navigate(['/dashboard']);
});

While this is my app.modules.ts

//setting up routes

const ROUTES = [
{
  path:'',
  redirectTo:'login',
  pathMatch:'full'
},
 {
   path:'login',
  component:LoginComponent
},{
  path:'dashboard',
    component:DashboardComponent,
  children:[
{
  path:'contacts',
  component:ContactsComponent
},{
  path:'home',
  component:HomeComponent,
}]
}];

and for my app.component.ts

<router-outlet></router-outlet>

But it doesn't even work, I have tried many solutions and check web, can't find the proper answer. The issue is my sidebar-component doesn't work

Expected behavior

I want when I navigate internally to my dashboard route, my component should work properly and be responsive.


Solution

  • put this script inside sidebar component...and then try loading..

    ngOnInit() {
        $(document).ready(() => {
          const trees: any = $('[data-widget="tree"]');
          trees.tree();
        });
      }