Last active
July 28, 2021 07:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const routeConfig:Routes = [ | |
{ | |
path: 'home', | |
component: Home | |
}, | |
{ | |
path: 'lessons', | |
component: AllLessons | |
} | |
]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector:'app', | |
template: ` | |
<main> | |
<router-outlet></router-outlet> | |
</main> | |
` | |
}) | |
export class App { | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {RouterModule} from "@angular/router"; | |
import {BrowserModule} from "@angular/platform-browser"; | |
@NgModule({ | |
declarations: [App], | |
imports: [BrowserModule, RouterModule.forRoot(routeConfig)], | |
bootstrap: [App] | |
}) | |
export class AppModule { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function redirectRouterLessonUnmatched(req,res) { | |
res.sendFile("index.html", { root: './index.html' }); | |
} | |
app.use(redirectRouterLessonUnmatched); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const routeConfig:Routes = [ | |
... , | |
{ | |
path: "", | |
component: Home | |
}, | |
{ | |
path: "**", | |
component: PageNotFoundComponent | |
} | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(private router:Router) { | |
} | |
openCourse(course) { | |
this.router.navigateByUrl(`/courses/${course.id}`); | |
} | |
showCoursePlayList(course) { | |
this.router.navigate(['/courses',course.id]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(router: Router) { | |
route.params.subscribe( | |
params =>{ | |
this.courseId = parseInt(params['id']); | |
} | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constructor(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
const parentRouteId = state.parent(route).params['id']; | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const routeConfig:Routes = [ | |
{ | |
path: '', | |
children: [ | |
{ | |
path: 'home', | |
component: Home | |
}, | |
{ | |
path: 'lessons', | |
component: AllLessons | |
} | |
] | |
} | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
] | |
} | |
] | |
} | |
]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const routeConfig:Routes = [ | |
{ | |
path: 'courses', | |
.... | |
}, | |
{ | |
path: 'playlist', | |
outlet: 'aside', | |
component: Playlist | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
03.ts imports the RouterModule twice, unnecessarily.