ANGULAR 4 路由问题:没有 HttpClient 的提供程序

ANGULAR 4 routing issue: No provider for HttpClient

您好,我有一个包含 link 到注册组件的登录组件。

当我点击这个 link 我遇到了这个问题:

ERROR Error: Uncaught (in promise): Error: No provider for HttpClient!
Error: No provider for HttpClient!
at injectionError (core.es5.js:1169)
at noProviderError (core.es5.js:1207)
at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._throwOrNull (core.es5.js:2649)
at ReflectiveInjector_.webpackJsonp.../../../core/@angular/co...

登录组件加载正常,注册失败

在登录组件上 html 我有:

 <a [routerLink]="['/register']" class="btn btn-link">Register</a>

我的应用程序路由如下所示:

 const appRoutes: Routes = [
{ path: '', component: HomeComponent, canActivate: [GuardService] },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },

// otherwise redirect to home
{ path: '**', redirectTo: '' }
];

export const routing = RouterModule.forRoot(appRoutes);

我使用的版本: Node.js版本:v8.5.0 NPM 版本:5.4.2

谢谢!

确保 HttpClientModuleimports 中列出:

@NgModule({
  imports: [BrowserModule, HttpClientModule, ...],
})
export class AppModule{}

另见 https://angular.io/api/common/http/HttpClient

确保 HttpModule 和路由在导入中列出。