youtube影片可以參考:
https://youtu.be/Uvj_7ZMrHmg
1.新增 app.router.ts
import { ModuleWithProviders } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { ServicesComponent } from './services/services.component';
// setting router path
export const router: Routes = [
{path: '', redirectTo: 'services', pathMatch: 'full' }, //redirect to service
{path: 'about', component: AboutComponent },
{path: 'services', component: ServicesComponent },
];
// generate router module
export const routes: ModuleWithProviders = RouterModule.forRoot(router);
2.app.module.ts 要import routes
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {routes} from './app.router'; //import
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { ServicesComponent } from './services/services.component';
@NgModule({
declarations: [
AppComponent,
AboutComponent,
ServicesComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routes //import
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
3.app.component.html 設定routerLink & router-outlet
//router-outlet就是所對應的component表現的地方
<router-outlet></router-outlet>