I am working on a webpage with a sub-navigation feature that displays subviews below a main view. I am looking for a way to pass an object to the subviews using the <router-outlet>
so that I only need to retrieve the data once in the main component and share it with the sub components.
Note: I have tried using the <one></one>
directive in the main.html and it works, but it is not the behavior I want.
Main View:
<h1>Details</h1>
<a [router-link]="['./sub1']">One</a> |
<a [router-link]="['./sub2']">Two</a> |
<a [router-link]="['./sub3']">Three</a>
<hr/>
<router-outlet [data]="maindata"></router-outlet>
Sub View 1:
<h2>{{ data.name }}</h2>
...
Main View:
@Component({
selector: 'main-detail',
directives: [ROUTER_DIRECTIVES],
templateUrl: './main.html'
})
@RouteConfig([
{ path: '/', redirectTo: '/one' },
{ path: '/one', as: 'One', component: OneComponent },
{ path: '/two', as: 'Two', component: TwoComponent },
{ path: '/three', as: 'Three', component: ThreeComponent }
])
export class MainComponent {
maindata: Object = {name:'jim'};
}
Sub View 1:
@Component({
selector: 'one',
directives: [CORE_DIRECTIVES],
inputs: ['data'],
templateUrl: './one.html'
})
export class OneComponent {
@Input() data;
}