I have a project with file structure
app/
(all the files that get automatically built) app-routing.module.ts components/
layout/ top/ side/ banner/ pages/ home/ prehistory/ prehuman/ australopithecine/ homininia/ earlyHuman/ outOfAfrica/ agriculture/ ancient/ (several directories like in prehistory) post-classical/ (several directories like in prehistory)
Each directory under pages/
was built in the CLI with
ng g c ___
app-routing.module.ts
the following code. Note that
since I'm at the early stages I haven't fully written out all the
children and their sub-children, I just wanted to get a small part of it
built and tested before building out the rest.app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './components/pages/home/home.component';
import { PrehistoryComponent } from './components/pages/prehistory/prehistory.component';
import { AncientComponent } from './components/pages/ancient/ancient.component';
export const routes: Routes = [
{path:'', redirectTo:'/home', pathMatch:'full'}
{path:'home', component:HomeComponent,
children: [
{path:'prehistory', component:PrehistoryComponent},
{path:'ancient', component:AncientComponent},
]}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
export class AppRoutingModule {}
In my AppComponent I used header and other tags to control CSS styles, but the important part is that there's a portion of the screen reserved for the header, for the side, and then the content which is the part that changes based on the address.
app.component.html
<header> <app-top></app-top> </header> <aside> <app-side></app-side> </aside> <content> <router-outlet></router-outlet> </content>
And the links are in the TopComponent.
top.component.html
<div><app-banner></app-banner></div>
<div id="breadcrumb">
<nav>
<a [routerLink]="['home']" routerLinkActive="active">Home</a>
<a (mouseover)="onHover()" [routerLink]="['./home','prehistory']" routerLinkActive="active">Prehistory</a>
</nav>
</div>
top.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-top',
templateUrl: './top.component.html',
styleUrls: ['./top.component.css'],
})
export class TopComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
onHover = function() {
console.log(this.route.parent);
}
ngOnInit() { }
}
When I navigate to `localhost:4200` it loads correctly, but clicking `Prehistory` doesn't load anything and the address becomes `localhost:4200/#/home/prehistory` I thought it should be `localhost:4200/#/home/#/prehistory` but perhaps I have that wrong. If I include a router outlet tag in the home or prehistory component then the prehistory page loads but it loads below the HomeComponent whereas I want it to replace the HomeComponent.
home.component.html
<p>
home works!
</p>
prehistory.component.html
<p>
prehistory works!
</p>
Using .forChild
instead of .forRoot
seems
to break the page.
Hi Frank,
The syntax you should use to navigate to prehistory is: [routerLink]="['./home/prehistory']"
things after the ,
are optional parameters usually.
Regards
Sander
Hi Frank,
I also noticed you used a <content>
tag. I’m not sure that’s in place there, I would use a <main>
Regards
Sander
Hi Adam
I just noticed the prefix .
, that’s probably the culprit. try [routerLink]="['/home/prehistory']"
Also, check the baseUrl in your project. That might mess things up too but is normally good in its default state.
regards
Sander