Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active July 28, 2021 07:28
Show Gist options
  • Save jhades/bd78526265196a36ac49f92288a22a2a to your computer and use it in GitHub Desktop.
Save jhades/bd78526265196a36ac49f92288a22a2a to your computer and use it in GitHub Desktop.
Angular Router Introduction: Child Routes, Auxiliary Routes, Master Detail - http://blog.angular-university.io/angular2-router
export const routeConfig:Routes = [
{
path: 'home',
component: Home
},
{
path: 'lessons',
component: AllLessons
}
];
@Component({
selector:'app',
template: `
<main>
<router-outlet></router-outlet>
</main>
`
})
export class App {
...
}
import {RouterModule} from "@angular/router";
import {BrowserModule} from "@angular/platform-browser";
@NgModule({
declarations: [App],
imports: [BrowserModule, RouterModule.forRoot(routeConfig)],
bootstrap: [App]
})
export class AppModule {
}
function redirectRouterLessonUnmatched(req,res) {
res.sendFile("index.html", { root: './index.html' });
}
app.use(redirectRouterLessonUnmatched);
export const routeConfig:Routes = [
... ,
{
path: "",
component: Home
},
{
path: "**",
component: PageNotFoundComponent
}
];
<ul class="top-menu">
<li>
<a routerLink="">Home</a>
</li>
<li>
<a routerLink="courses">Courses</a>
</li>
<li>
<a [routerLink]="['lessons']">Lessons</a>
</li>
</ul>
constructor(private router:Router) {
}
openCourse(course) {
this.router.navigateByUrl(`/courses/${course.id}`);
}
showCoursePlayList(course) {
this.router.navigate(['/courses',course.id]);
}
constructor(router: Router) {
route.params.subscribe(
params =>{
this.courseId = parseInt(params['id']);
}
);
}
constructor(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const parentRouteId = state.parent(route).params['id'];
...
}
export const routeConfig:Routes = [
{
path: '',
children: [
{
path: 'home',
component: Home
},
{
path: 'lessons',
component: AllLessons
}
]
}
];
export const routeConfig:Routes = [
{
path: 'courses',
children: [
{
path: ':id',
children: [
{
path: '',
component: CourseLessons,
},
{
path: 'videos/:id',
component: VideoLesson
},
{
path: 'textlectures/:id',
component: TextLesson
},
{
path: 'quizzes/:id',
component: QuizLesson
},
{
path: 'interactivelessons/:id',
component: InteractiveLesson
}
]
}
]
}
];
<div class="main-container">
<router-outlet></router-outlet>
<router-outlet name="section1"></router-outlet>
<router-outlet name="section2"></router-outlet>
<router-outlet name="section3"></router-outlet>
</div>
export const routeConfig:Routes = [
{
path: 'courses',
....
},
{
path: 'playlist',
outlet: 'aside',
component: Playlist
}
];
@samuraicode
Copy link

03.ts imports the RouterModule twice, unnecessarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment