I think I had a similar dilemma, and thatβs how I deal with such situations.
1. Target and input parameters
When I want to break down a child component from the main component, I will try to see if this is a different kind of system object or not (basically, if I need a full routing for it or I can solve my data link using only the property as the target / input) Do not forget:
Angular insists that we declare the target property as the input property. If we do not, Angular will reject the binding and throw an error.
Check out part3 from the ng2 tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt3.html
1. Routing
For different components, you can define a new route and then retrieve data using two methods:
1.1. From the parameters of the route and the service (you should use the service method for any data loading, so you can reorganize access to the data and keep the component aside and focus on supporting the view). You should use route parameters only for filters or tasks of small tasks, and not for data transmission. Here is a possible component:
Object1Routes
import { RouterConfig } from '@angular/router'; import { Object1Dashboard } from './object1.dashboard'; import { Object1Edit } from './object1.edit'; export const Object1Routes: RouterConfig = [ { path: 'object1', component: Object1Dashboard, 'children': [ <...> ,{ path: 'edit/:id', component: Object1Edit } ] } ];
Object1Edit
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { Object1Service } from './services/object1/object1.service'; import { Object1Model } from './models/object1/object1.model'; @Component({ selector: 'object1-edit', templateUrl: './object1/object1.edit.html', directives: [] }) export class Object1Edit implements OnInit, OnDestroy { model = new Object1Model; sub:any; editId:number; constructor( private route: ActivatedRoute, private router: Router, private serviceData: Object1Service ) { } onSubmit(d:Object1Model) { this.model = d; this.router.navigate(['/object1']); } ngOnInit() { this.sub = this.route.params.subscribe(params => { this.editId = +params['id'];
1.2. Just out of service
Object1Routes
import { RouterConfig } from '@angular/router'; import { Object1Dashboard } from './object1.dashboard'; import { Object1List } from './object1.list'; export const Object1Routes: RouterConfig = [ { path: 'object1', component: Object1Dashboard, 'children': [ { path: '', component: Object1List } <...> ] } ];
Object1List
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { Object1Service } from './services/object1/object1.service'; import { Object1Model } from './models/object1/object1.model'; @Component({ selector: 'object1-list', templateUrl: './object1/object1.list.html' }) export class Object1List implements OnInit, OnDestroy { constructor( private route: ActivatedRoute, private router: Router, private serviceData:Object1Service ) { } modelArray:Object1Model[]; selectedId:number; private sub: any; onSelect(model:Object1Model) { console.log('Select ' + model.code); let link = ['/object1/edit', model.id]; this.router.navigate(link); } onDelete(model:Object1Model) { console.log('Delete : ' + model.code); this.serviceData.delObject1ById(model.id); } ngOnInit() { this.sub = this.route .params.subscribe(params => { this.selectedId = +params['id']; this.serviceData.getAllObject1().then(data => this.modelArray = data); }); } ngOnDestroy() { if (this.sub) { this.sub.unsubscribe(); } } }
Hope this helps. Let me know if you have a different opinion or if I missed something.
The code provided is based on Angular 2.0.0-rc.2 and @ angular / router 3.0.0-alpha.7.
Also check out this article on the principle of single responsibility : https://blog.8thlight.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html