<!DOCTYPE html>
<html>
<html>
<head>
<!-- Set the base href -->
<script>document.write('<base href="' + document.location + '" />');</script>
<title>Router Sample</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<!-- Polyfill(s) for older browsers -->
<script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app>loading...</my-app>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/* Styles go here */
/**
* PLUNKER VERSION (based on systemjs.config.js in angular.io)
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function(global) {
var ngVer = '@2.4.7'; // lock in the angular package version; do not let it float to current!
var routerVer = '@3.4.7'; // lock router version
//map tells the System loader where to look for things
var map = {
'app': 'app',
'@angular': 'https://npmcdn.com/@angular', // sufficient if we didn't pin the version
'@angular/router': 'https://npmcdn.com/@angular/router' + routerVer,
'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6',
'ts': 'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'https://npmcdn.com/typescript@1.9.0-dev.20160409/lib/typescript.js',
};
//packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'app.ts', defaultExtension: 'ts' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
var ngPackageNames = [
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router-deprecated',
'upgrade',
];
// Add map entries for each angular package
// only because we're pinning the version with `ngVer`.
ngPackageNames.forEach(function(pkgName) {
map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
});
// Add package entries for angular packages
ngPackageNames.forEach(function(pkgName) {
// Bundled (~40 requests):
packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
// Individual files (~300 requests):
//packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
// No umd for router yet
packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };
var config = {
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
map: map,
packages: packages
}
System.config(config);
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {BrowserModule} from '@angular/platform-browser';
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
import {NgModule, Component} from '@angular/core';
import {AppRoutingModule} from './app.routes';
import {ListComponent} from './list/list.component.ts';
import {AComponent, BComponent, CComponent, DComponent} from './routes/components.ts';
@Component({
selector: 'my-app',
template: `<h1> angular2 example</h1>
{{win?.location.href}}
<router-outlet></router-outlet>`
})
export class MyAppComponent {
private win = window;
}
@NgModule({
imports: [AppRoutingModule,
BrowserModule], // module dependencies
declarations: [
MyAppComponent,
ListComponent,
AComponent,
BComponent,
CComponent,
DComponent
], // components and directives
bootstrap: [MyAppComponent]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
import {Component} from '@angular/core';
import {ActivatedRoute, Router, ROUTER_DIRECTIVES} from '@angular/router';
@Component({
selector: 'list',
directives: [ROUTER_DIRECTIVES],
template: `
<p><a [routerLink]="['/a', '']">Component A</a></p>
<p><a [routerLink]="['../b', '']">Component B</a></p>
<p><a [routerLink]="['../../c', '']">Component C</a></p>
<p><a href="javascript:void(0);" (click)="navigateToD()">Component D</a></p>
`
})
export class ListComponent {
constructor (private router: Router) {
}
navigateToD(){
this.router.navigate(['/d', '']);
}
}
import {NgModule, Component} from '@angular/core';
import {provideRouter, RouterConfig, RouterModule, UrlSegment} from '@angular/router';
import {ListComponent} from './list/list.component';
import {AComponent, BComponent, CComponent, DComponent} from './routes/components';
let routes: RouterConfig = [
{
path: 'a/',
component: AComponent
},
{
path: 'list/b/',
component: BComponent,
},
{
path: 'c/',
component: CComponent,
},
{
path: 'd/',
component: DComponent,
},
{
path: 'list/',
component: ListComponent,
},
{
path: '**',
redirectTo: 'list/',
terminal: true
}
];
export const APP_ROUTER_PROVIDERS = RouterModule.forRoot(routes);
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
import {Component} from '@angular/core';
import {ActivatedRoute, Router, ROUTER_DIRECTIVES} from '@angular/router';
@Component({
selector: 'a-dir',
directives: [ROUTER_DIRECTIVES],
template: `
<h1>A</h1>
<p><a [routerLink]="['/list']">Back</a></p>
`
})
export class AComponent {}
@Component({
selector: 'b-dir',
directives: [ROUTER_DIRECTIVES],
template: `
<h1>B</h1>
<p><a [routerLink]="['/list']">Back</a></p>
`
})
export class BComponent {}
@Component({
selector: 'c-dir',
directives: [ROUTER_DIRECTIVES],
template: `
<h1>C</h1>
<p><a [routerLink]="['/list']">Back</a></p>
`
})
export class CComponent {}
@Component({
selector: 'd-dir',
directives: [ROUTER_DIRECTIVES],
template: `
<h1>D</h1>
<p><a [routerLink]="['/list']">Back</a></p>
`
})
export class DComponent {}