Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angular - How to use home component data in other component?

Below is my code, how to use (this.categoryType) in other component

getCategoryDetails(){
return this.http.get('ip/ramu/api/api/…')
.map((res:Response) => res.json());
}

Above code I am using in webservice.ts

ngOnInit() { this.wsp.getCategoryDetails().subscribe(data => {
this.categoryType = data; });
}

Above code I have used in home.ts, but I want use this response in other component.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a service to save the category. And then you can use the same service to get the categoryType whenever you want it.

import { Injectable } from '@angular/core';
@Injectable()
export class AppMessageQueuService {

    categoryType: string;
    constructor() {

    }
    saveCateogry(cat:any){
        this.categoryType= cat;
    }
    retrieveCategory(){
        return this.categoryType;
    }

}

Then you can inject this Service into both the components and save it from the first component like,

this.appMsgService.saveCateogry(yourcategory);

then retrieve in the second component as,

this.appMsgService.retrieveCategory();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...