How to use home component data in other component?

前端 未结 1 550
挽巷
挽巷 2020-12-20 10:20

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

getCategoryDetails(){
return this.http.get(\'ip/ramu/api/api/…\')
.map((res:Response) =&g         


        
相关标签:
1条回答
  • 2020-12-20 10:40

    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();
    
    0 讨论(0)
提交回复
热议问题