簡體   English   中英

Angular2中的父子組件通信

[英]Parent-child component communication in Angular2

我在子組件ts文件中編寫了一個名為“ test”的變量。 測試值將從子組件視圖更新。 我必須訪問父組件中的“測試”變量。

共享服務似乎是您所需要的。 在此處查看以下答案: 共享服務您將像在該答案中那樣進行共享服務,在此共享test並可以從所需的組件中訪問它:)

@Injectable()
export class YourSharedService {
    sharedTest: {
        testString: 'your value here'
    };
}

然后將其注入構造函數中並在組件中進行訪問:

constructor(private yourSharedService: YourSharedService) {  }

testString = this.yourSharedService.sharedTest

正如Suraj所說,只需檢查他提供的鏈接,這里有一些解決方案的技巧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM