How do I pass data to Angular routed components?

前端 未结 16 1401
挽巷
挽巷 2020-11-22 08:03

In one of my Angular 2 routes\'s templates (FirstComponent) I have a button

first.component.html

16条回答
  •  甜味超标
    2020-11-22 08:47

    use a shared service to store data with a custom index. then send that custom index with queryParam. this approach is more flexible.

    // component-a : typeScript :
    constructor( private DataCollector: DataCollectorService ) {}
    
    ngOnInit() {
        this.DataCollector['someDataIndex'] = data;
    }
    
    // component-a : html :
    
    

    .

    // component-b : typeScript :
    public data;
    
    constructor( private DataCollector: DataCollectorService ) {}
    
    ngOnInit() {
        this.route.queryParams.subscribe(
            (queryParams: Params) => {
                this.data = this.DataCollector[queryParams['index']];
            }
        );
    }
    

提交回复
热议问题