NullInjectorError仅在路由解析器上

时间:2020-08-23 07:05:12

标签: angular angular2-routing

我在功能模块中声明了一个带有路由模块的组件

import { TestResolverService } from './service/test.resolver.service';
import { MyComponent } from './containers/my-component/mycomponent.component';


const routes: Routes =
  [
    {
        path: '',
        component: MyComponent,
        resolve: TestResolverService
    },
  ];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class MyRoutingModule { }

TestResolverService在此处的同一模块中定义:

@Injectable({
  providedIn: 'root'
})
export class TestResolverService implements Resolve<void>{

  constructor() { }
  resolve(x, y) {}
}

该模块是从根目录懒加载的:

{path: 'mymodule', loadChildren: () => import('./features/test/mymodule.module').then(m => m.MyModule)}

每当我尝试将resolve加载到页面时,都会出现以下错误:

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(PathwaysModule)[TestResolverService_Factory -> TestResolverService_Factory -> TestResolverService_Factory -> TestResolverService_Factory]: 
  NullInjectorError: No provider for TestResolverService_Factory!
NullInjectorError: R3InjectorError(MyModule)[TestResolverService_Factory -> TestResolverService_Factory -> TestResolverService_Factory -> TestResolverService_Factory]: 
  NullInjectorError: No provider for TestResolverService_Factory! 

我想念什么?没有resolve的情况下,页面可以正常加载,并且直接从组件导入和调用服务也可以。只有当作为解析器引用时,我才会遇到此问题。

1 个答案:

答案 0 :(得分:0)

我能够通过将resolve: TestResolverService更改为

来解决此问题
resolve: { resolver: TestResolverService }

我的解决类型错误。