//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HashLocationStrategy,LocationStrategy,CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { WellcomeComponent } from './wellcome/wellcome.component';
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'wellcome', component: WellcomeComponent },
{path: '**', redirectTo: '', pathMatch: 'full' }
]
@NgModule({
declarations: [
AppComponent,
HomeComponent,
WellcomeComponent
],
imports: [
BrowserModule,
CommonModule,
RouterModule.forRoot(appRoutes)
],
providers: [
{ provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
the app.component.ts
//app.component.ts
import { Component } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
@Component({
selector: 'app-root',
template:`
<div style="text-align:center">
<p>
<a [routerLink]="['/']">Home</a>
</p>
<p>
<a [routerLink]="['/wellcome']">Wellcome</a>
</p>
<h1>
Welcome to {{title}}!
</h1>
</div>
<h2>Here are router-outlet: </h2>
<router-outlet></router-outlet>`,
})
export class AppComponent {
title = 'app';
}
the wellcome.component.ts
//wellcome.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-wellcome',
template:`
<p>
wellcome works!
</p>
`
})
export class WellcomeComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
the home.component.ts
//home.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
template:`
<p>
home works!
</p>
`
})
export class HomeComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
after you do ng build --prod you must edit the index.html replacing by document.write("")
Index.html will be like
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AppNew</title>
<script>document.write("<base href='"+window.location.href+"'/>") </script>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="styles.d41d8cd98f00b204e980.bundle.css" rel="stylesheet"/>
</head>
<body>
<app-root/>
<script type="text/javascript" src="inline.2833f8af5a9f1ba869e4.bundle.js"/>
<script type="text/javascript" src="polyfills.8eba0ab53b5457105d75.bundle.js"/>
<script type="text/javascript" src="vendor.aeea1adcdc349547d6f1.bundle.js"/>
<script type="text/javascript" src="main.c39d8a271dce22bb4c5c.bundle.js"/>
</body>
</html>