我有决心从api和依赖于该解析的服务中获取数据。取决于解决方案的服务将为多个组件提供数据部分,并且仅在页面加载时运行一次。这是相关代码(导入和一些注释省略):
产品details.service.ts
@Injectable()
export class ProductDetailsService {
private productDetails: ProductDetails;
constructor(route: ActivatedRoute) {
this.productDetails = route.snapshot.data['product'];
}
public getProductDetails(): ProductDetails {
return this.productDetails;
}
}
产品details.resolve.ts
@Injectable()
export class ProductDetailsResolve implements Resolve<ProductDetails> {
constructor(private httpService: HttpService) { }
resolve(route: ActivatedRouteSnapshot): Observable<ProductDetails> {
return this.httpService.getProductDetails(route.params['id']);
}
}
APP-routing.module.ts
const routes: Routes = [
{ path: 'main/:id', component: MainComponent, resolve: { product: ProductDetailsService } },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
some.component.ts
export class SomeComponent {
public value: number = 0;
constructor(private productDetailsService: ProductDetailsService) {
this.value = productDetailsService.getProductDetails().value;
}
}
当我运行应用程序时,我收到此错误:
core.es5.js:1020 ERROR Error: Uncaught (in promise): TypeError: resolver is not a function
TypeError: resolver is not a function
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.getResolver (router.es5.js:4559)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.resolveNode (router.es5.js:4537)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.runResolve (router.es5.js:4518)
at MergeMapSubscriber.project (router.es5.js:4285)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:120)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:110)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
at ArrayObservable.webpackJsonp.../../../../rxjs/observable/ArrayObservable.js.ArrayObservable._subscribe (ArrayObservable.js:114)
at ArrayObservable.webpackJsonp.../../../../rxjs/Observable.js.Observable._trySubscribe (Observable.js:171)
at ArrayObservable.webpackJsonp.../../../../rxjs/Observable.js.Observable.subscribe (Observable.js:159)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.getResolver (router.es5.js:4559)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.resolveNode (router.es5.js:4537)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.runResolve (router.es5.js:4518)
at MergeMapSubscriber.project (router.es5.js:4285)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:120)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/operator/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:110)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
at ArrayObservable.webpackJsonp.../../../../rxjs/observable/ArrayObservable.js.ArrayObservable._subscribe (ArrayObservable.js:114)
at ArrayObservable.webpackJsonp.../../../../rxjs/Observable.js.Observable._trySubscribe (Observable.js:171)
at ArrayObservable.webpackJsonp.../../../../rxjs/Observable.js.Observable.subscribe (Observable.js:159)
at resolvePromise (zone.js:795)
at resolvePromise (zone.js:766)
at zone.js:844
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.es5.js:3881)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at <anonymous>
对这个错误的研究并没有给我任何线索,我无法想出为什么决心不被承认为一个功能的任何理由。我在这里找不到什么东西吗?
答案 0 :(得分:0)
您需要将ProductDetailsResolve
声明为AppRoutingModule
的提供者,如下所示:
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [ProductDetailsResolve]
})
记住要在您的ProductDetailsResolve
上导入AppRoutingModule
答案 1 :(得分:0)
当我在路由配置中不小心通过了Guard
作为解析器时,出现了此错误:
resolve: { data: MyGuardService }
因此,请检查您是否意外通过了错误的课程。
答案 2 :(得分:0)
我在Angular 8中收到此错误,因为我忘记实施Resolve<T>
接口在我的解析器类中。即我必须更改:
@Injectable({
providedIn: 'root'
}
export class FooResolverService {
// ...
}
到
@Injectable({
providedIn: 'root'
}
export class FooResolverService implements Resolve<Foo> {
// ...
}