ANGULAR 4路由问题:没有HttpClient

时间:2017-09-29 12:04:24

标签: angular angular-httpclient

您好我有一个登录组件,其中包含指向寄存器组件的链接。

当我点击此链接时,我遇到了这个问题:

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

谢谢!

2 个答案:

答案 0 :(得分:8)

确保HttpClientModule中列出了imports

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

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

答案 1 :(得分:1)

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