如果是简单数据,您可以传递它们路由参数 https://angular.io/docs/ts/latest/api/router/RouteParams-class.html
<a [router-link]="['./sub3'],{name:'jim'}">Three</a>
然后在你的子视图中
@Component({
selector: 'one',
directives: [CORE_DIRECTIVES],
templateUrl: './one.html'
})
export class OneComponent {
data: any;
constructor(params: RouteParams){
this.data = params.get('data');
}
}
您还可以通过将 RouterConfig 移动到组件内部来设置路由始终从组件传递参数(注意,这不是通常的做法):
export class AppCmp {
history: string[] = [];
constructor(public list: PersonalizationList,
private router_: Router) {
list.get('histoy', (response) => {
this.history = response;
});
router_.config([
{ path: '/', component: HomeCmp, as: 'Home', data: this.history },
{ path: '/about', component: AboutCmp, as: 'About' }
]);
}
}
归功于来源 https://stackoverflow.com/a/34290879/1251604
如果您要做一些更复杂的事情,我建议使用服务在路由/组件之间进行通信。这实际上是我更喜欢的方式。
样品服务:
import {Injectable} from 'angular2/angular2';
@Injectable()
export class CarsService {
list1: array<any> = ['a','b','c','d'];
list2: array<any>;
constructor() {
this.list2 = [1,2,3,9,11];
}
}
如何注入服务:
export class Cars {
constructor(cars:CarsService) {
this.cmpList1 = cars.list1;
this.cmpList2 = cars.list2;
}
}
这样,您就可以使用该服务进行通信,而无需考虑父母/孩子或其他奇怪的限制。