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));
这似乎是做伎俩,但它是如何它应该做?
有趣的问题,是什么使这样的通用服务的优势在哪里? –
这个想法是为接收者提供一个通用参数,以防我想为不同的应用使用这种服务,例如应用栏标题可能取决于我正在浏览的视图并从事件提供的参数中获取其文本。该服务应该不知道实际参数类型是什么。 –