这是服务:
import { Injectable } from '@angular/core';
import { Router, Event, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
@Injectable()
export class LoaderService {
public shouldShowLoader: boolean = false;
constructor(private router: Router) {
router.events.subscribe((event: Event) => {
this.navigationInterceptor(event);
});
}
// Shows and hides the loading spinner during Event changes
navigationInterceptor(event: Event): void {
if (event instanceof NavigationStart) {
this.shouldShowLoader = true;
}
else if (event instanceof NavigationEnd) {
this.shouldShowLoader = false;
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
else if (event instanceof NavigationCancel) {
this.shouldShowLoader = false;
}
else if (event instanceof NavigationError) {
this.shouldShowLoader = false;
}
else {
this.shouldShowLoader = false;
}
}
}

这是失败的测试:
import { TestBed, inject } from '@angular/core/testing';
import { Router } from '@angular/router';
import { LoaderService } from './loader.service';
describe('LoaderServiceTest', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ LoaderService, Router ]
});
});
it('#LoaderService should be defined', inject([LoaderService, Router], (service: LoaderService) => {
expect(service).toBeDefined();
}));
});

不确定为什么会失败?谷歌搜索类似的问题,我只能找到Angular 2 beta的答案...我们正在使用最新的Angular 2 / 2.2.0