2016-06-10 24 views
1

讓我們假設我有一個像如何在Angular2引導階段正確提供通用服務?

@Injectable() 
export class NavigationService<T> { 
    navigated: EventEmitter<T> = new EventEmitter<T>(); 
    navigate(view: T) { 
     this.navigated.emit(view); 
    } 
} 

,我想在我的應用引導提供這項服務的通用服務,我該怎麼做呢?我玩了一些,並想出了

import { NavigationService } from './navigation.service'; 
import { View } from './view'; 

bootstrap(AppComponent, [ 
    provide(NavigationService, { useValue: new NavigationService<View>() ]) 
    .then(success => console.log('Bootstrap success')) 
    .catch(error => console.log(error)); 

這似乎是做伎倆,但它是如何它應該做?

+0

有趣的問題,是什麼使這樣的通用服務的優勢在哪裏? –

+0

這個想法是爲接收者提供一個通用參數,以防我想爲不同的應用使用這種服務,例如應用欄標題可能取決於我正在瀏覽的視圖並從事件提供的參數中獲取其文本。該服務應該不知道實際參數類型是什麼。 –

回答

0

像這樣:

bootstrap(AppComponent, [ NavigationService]) 
+0

泛型不重要? –