2016-10-21 47 views
0

我有一個服務(MyService),我包含在我的項目模塊中。 我在每個組件中都創建了一個類型爲MyService的私有變量來設置和獲取該服務中的數據。爲什麼angular2服務被多次調用?

在我的控制檯日誌中,我看到相同的服務被多次調用。

@NgModule({ 
    imports: [ 
    BrowserModule, 
    routing, 
    ], 
    declarations: [ 
    AppComponent 
    ], 
    providers: [ 
    MyService, 
    ], 
    bootstrap: [AppComponent] 
}) 


//Here is my Component code which create a private variable in my component 

@Component({ 
    selector: 'summary', 
    templateUrl: 'summary.component.html' 
}) 
export class SummaryComponent extends OnInit { 

    constructor(
       private myService: MyService, 
      ) { 
    super(); 
    } 

我該如何多次停止對象創建?

+0

調用服務的代碼在哪裏?什麼方法被稱爲幾次?您的網頁上有多少個彙總組件? –

+0

我剛纔給出了一個組件,並且有很多這樣的組件。在我的每個組件的應用程序中,我們創建了MyService類型變量,並將其注入構造函數中,如代碼中所示。 –

+0

這不能回答我的問題 –

回答

相關問題