2016-12-28 85 views
0

因此,我有一個產品「父」頁,其中有3個路由器鏈接,將頁面的一部分更改爲例如「孩子」概覽頁面,圖庫頁面或規格頁面。通過路由器插座「子」組件傳遞父組件變量

現在,有關產品的信息由變體「產品」中的父級產品頁面(詳細信息,指向圖像和規格的鏈接)保存。如何從我的routeroutlet使用的子組件中訪問父組件的「產品」變量?

+0

你曾經通過@input試過嗎? –

+0

使用「服務」,「事件」等 – anshuVersatile

+0

@federicoscamuzzi我沒有使用組件選擇器,但路由器插座標籤,它不支持任何輸入變量 – TanguyB

回答

0

服務

@Injectable() 
export class AppService{ 
    private mainSource = new Subject<boolean>(); 
    main$ = this.mainSource.asObservable(); 

    setWorking(isWorking: boolean) { 
    this.mainSource.next(isWorking); 
    } 
} 

組件

private subscription: Subscription 

constructor(private appService: AppService){ } 

ngOnInit(){ 

    this.appService.setWorking(false); 

    this.subscription = this.appService.main$.subscribe(isWorking => { 
    if (isWorking) { alert("Tio"); } 
    } 
} 

ngOnDestroy() { 
    this.subscription.unsubscribe(); 
} 
相關問題