第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數(shù)據(jù)傳遞到“路由器出口”子組件

將數(shù)據(jù)傳遞到“路由器出口”子組件

滄海一幻覺 2019-12-26 13:47:24
我有一個去服務(wù)器的父組件并獲取一個對象:// parent component@Component({    selector : 'node-display',    template : `        <router-outlet [node]="node"></router-outlet>    `})export class NodeDisplayComponent implements OnInit {    node: Node;    ngOnInit(): void {        this.nodeService.getNode(path)            .subscribe(                node => {                    this.node = node;                },                err => {                    console.log(err);                }            );    }在以下幾種子顯示之一中:export class ChildDisplay implements OnInit{    @Input()    node: Node;    ngOnInit(): void {        console.log(this.node);    }}似乎我無法將數(shù)據(jù)注入router-outlet??磥砦以赪eb控制臺中收到錯誤:Can't bind to 'node' since it isn't a known property of 'router-outlet'.這多少有些道理,但是我將如何執(zhí)行以下操作:從父組件中獲取服務(wù)器中的“節(jié)點”數(shù)據(jù)?將我從服務(wù)器檢索到的數(shù)據(jù)傳遞到子路由器出口?似乎router-outlets工作方式不同。
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

Günters的回答很好,我只想指出另一種不使用Observables的方式。


在這里,盡管我們必須記住這些對象是通過引用傳遞的,所以如果您想對子對象中的對象做一些工作而不影響父對象,我建議使用Günther解決方案。但是,如果這無關(guān)緊要,或者實際上是期望的行為,我將建議以下內(nèi)容。


@Injectable()

export class SharedService {


    sharedNode = {

      // properties

    };

}

在您的父母中,您可以分配值:


this.sharedService.sharedNode = this.node;

并在您的孩子(和父母)中,將共享服務(wù)注入到您的構(gòu)造函數(shù)中。如果要在該模塊中的所有組件上使用單例服務(wù),請記住在模塊級別的提供程序數(shù)組中提供服務(wù)。另外,只需添加服務(wù)父的供應(yīng)商陣列中只,那么家長和孩子將共享相同的服務(wù)實例。


node: Node;


ngOnInit() {

    this.node = this.sharedService.sharedNode;    

}

正如紐曼所指出的,您還可以this.sharedService.sharedNode在html模板中或使用getter:


get sharedNode(){

  return this.sharedService.sharedNode;

}


查看完整回答
反對 回復(fù) 2019-12-26
  • 3 回答
  • 0 關(guān)注
  • 1042 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號