2017-01-23 10 views
1

我一直在嘗試在組件之間共享數據,並遵循here的建議來創建商店。如何允許組件中的函數訪問在構造函數中導入的工廠?

這一切都從我的組件構造函數很好,但我不知道如何給函數類似訪問。

class Home { 
    constructor($scope, $reactive, Store) { 
    'ngInject'; 

    $reactive(this).attach($scope); 

    this.selectedDate = Store.get().selectedDate; 
} 

這所有的作品,但這裏訪問存儲不起作用:

nextDay(){ 
    'ngInject'; 
    Store.set({selectedDate: moment(this.selectedDate).add(1, 'd').format('YYYY-MM-DD')}); 
    console.log('nextDay'); 
} 

我試圖連接商店$反應,我已經試過this.Store並通過存儲爲agrument到nextDay(),但無法弄清楚。任何幫助將不勝感激。

感謝

回答

1

你應該在類

例如指定服務(注射的東西),如果你想使用存儲分配this.Store =商店 ,然後你可以從所有的方法都使用它作爲此類的商店。商店

class Home { 
     constructor($scope, $reactive, Store) { 
     'ngInject'; 
     this.Store = Store; 
     this.$reactive = $reactive; 
     this.$scope = $scope; 
     this.$reactive(this).attach($scope); 

     this.selectedDate = this.Store.get().selectedDate; 
     } 

     nextDay(){ 
     this.Store.set({selectedDate: moment(this.selectedDate).add(1, 'd').format('YYYY-MM-DD')}); 
     console.log('nextDay'); 

     } 
    } 
+0

謝謝,我認爲這將是簡單的事情! – enfrost

相關問題