I have been working on a project with Angular 2 where I am utilizing ROUTER_DIRECTIVES
to move between components.
There are currently two components involved, namely PagesComponent
and DesignerComponent
.
My goal is to navigate from PagesComponent to DesignerComponent.
Although the routing is working as expected, I am facing an issue where I need to pass the page
Object to the designer component so it can load that specific page.
I attempted to use RouteParams but encountered a problem where the page object was returning as undefined
.
Here is a snippet of my code:
pages.component.ts
import {Component, OnInit ,Input} from 'angular2/core';
import { GlobalObjectsService} from './../../shared/services/global/global.objects.service';
import { ROUTER_DIRECTIVES, RouteConfig } from 'angular2/router';
import { DesignerComponent } from './../../designer/designer.component';
import {RouteParams} from 'angular2/router';
@Component({
selector: 'pages',
directives:[ROUTER_DIRECTIVES,],
templateUrl: 'app/project-manager/pages/pages.component.html'
})
@RouteConfig([
{ path: '/',name: 'Designer',component: DesignerComponent }
])
export class PagesComponent implements OnInit {
@Input() pages:any;
public selectedWorkspace:any;
constructor(private globalObjectsService:GlobalObjectsService) {
this.selectedWorkspace=this.globalObjectsService.selectedWorkspace;
}
ngOnInit() { }
}
In the html, I am implementing the following:
<scrollable height="300" class="list-group" style="overflow-y: auto; width: auto; height: 200px;" *ngFor="#page of pages">
{{page.name}}<a [routerLink]="['Designer',{page: page}]" title="Page Designer"><i class="fa fa-edit"></i></a>
</scrollable>
Within the constructor of DesignerComponent, I have included the following code:
constructor(params: RouteParams) {
this.page = params.get('page');
console.log(this.page);//undefined
}
While the routing is functioning correctly to the designer component, I am encountering an issue when trying to access the page
Object within the designer, as it is displaying as undefined
.
Any suggestions or solutions?