2016-12-14 69 views
1

我有兩個變量goalsDatagoals其初始負載正常工作,但我想保持goal同步與goalsData所以,如果有上goalsData該改變的改變一直持續到goals如何我可以做到這一點嗎?保持兩個值同步數據改變時

ngOnInit() { 
    this.goalsData = this.apollo.watchQuery({ query: GoalQuery, forceFetch: true }); 
    this.goals = this.goalsData.map(({data}) => data.allGoals); 

    console.log(this.goals); 
    } 
+0

你可以用'Subject'。 – Emu

回答

0

我沒有使用Apollo,但watchQuery返回一個Observable。這意味着你必須訂閱它才能獲得更改。

ngOnInit() { 
    this.goalsData = this.apollo.watchQuery({ query: GoalQuery, forceFetch: true }); 
    this.goalsData 
     .map({data}=>data.allGoals) 
     .subscribe((goals)=>{ 
      this.goals = goals; 
      console.log(this.goals); 
     },(err)=>{ 
      console.error(err); 
     },()=>{ 
      console.log('finished'); 
     }); 
} 

假設阿波羅將發射目標數據,這將不斷更新this.goals