I have an angular 5 web app by which i need to do routing on a separate in a separate component.
This is my project structure:
- /app
- /app/app.module.ts
- /app/app-routing.module.ts
- /app/app.component.html
- /pages/help/help1
- /pages/help/help2
- /pages/home
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { Help1Component } from '...';
import { HomeComponent } from '...';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
Help1Component
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from '...';
import { Help1Component } from '...';
const routes: Routes = [
{
path: 'help1',
component: Help1Component,
outlet: 'home'
},
{
path: '',
component: HomeComponent
}
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
app.component.html:
<router-outlet></router-outlet>
home.component.html:
<ul class="sub">
<li><a href="javascript:void(0);" (click)="helpPages('help1')"></li>
<other links here>
</ul>
<some other html>
<router-outlet name="home"></router-outlet>
<some other html>
home.component.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor(
private _router: Router) { }
ngOnInit() {}
helpPages(routeName:string) {
this._router.navigate([{ outlets: { home: [ routeName ] }}])
}
}
The home component has a named router-outlet (home) that is used to load content pages from my navigation menu. So whenever i click for example a help menu, it should load that on that router-outlet on the home.component, and not on the main one on app.component
Then on my home.component.ts, i have this code snippet:
helpPages(routeName:string) {
this._router.navigate([{ outlets: { home: [ routeName ] }}])
}
Where routeName could be any route that has been mapped to home, in this case, the help pages.
{
path: 'help1',
component: Help1Component,
outlet: 'home'
}
But when invoking the method, i instead get an error:
SyntaxError: Unexpected token )
by which i do not understand. I am sure i have my codes correct and compiles well with no issues. I have been bangin' my head on this for a few days already, checking every tutorial, forums, and stack overflowing for possible solution, but to no avail.
But when i put the <router-outlet name="home"> on app.component.html, just below the other router-outlet, the page loading works.
What i'm not getting is that why it does not work when its on a separate component other than app.component.html.
I even tried importing NgModule, Router, Routes, and RouterModule into my home.component, but still does not work.
Thanks advance for any help.
UPDATE: the issue of SyntaxError: Unexpected token ) was a chrome browser issue attaching # or :1 at the end of the supposed call. This has been tagged as bug in Chrome v65 and should be fixed in v66 as per google post.