asp.net-mvc - NullInjectorError : No provider for HttpClient! Angular 5

标签 asp.net-mvc angular typescript asp.net-core

我在 Visual Studio 2017 中使用 Angular 模板。然后我更新到 Angular 5.2。我,试图找到解决方案。但没有得到确切的解决方案。

服务类正在调用 http 调用。但是,我收到错误消息

error message

服务.TS

import { Injectable } from '@angular/core';
import { LoginViewModel as loginVM } from "../../viewmodel/app.viewmodel"
import { HttpClient, HttpHeaders } from "@angular/common/http";

@Injectable()
export class LoginService {
    private loginUrl = "Account/Authentication";
    private _httpClientModule: HttpClient;
    constructor(httpClientModule: HttpClient) {
        this._httpClientModule = httpClientModule;
    }

    public LoginHttpCall(_loginVM: loginVM) {

        const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');

        this._httpClientModule.post(this.loginUrl, _loginVM, { headers }).
            subscribe(data => {
                console.log(data);
            },
            err => {
                console.log("Error occured.");
            });
    }

}

这是我的组件类
import { Component } from '@angular/core';
import { AppComponent } from "../app/app.component";
import { LoginService } from "../../service/account/app.service.account.login";
import { LoginViewModel } from "../../viewmodel/app.viewmodel";
declare var componentHandler: any;
@Component({
    selector: 'login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css'],
    providers: [LoginViewModel, LoginService]
})
export class LoginComponent {
    private _appComponent: AppComponent;
    private _loginService: LoginService;
    constructor(private appComponent: AppComponent, loginService: LoginService) {
        this._appComponent = appComponent;
        this._appComponent.menulist = false;
        this._loginService = loginService;
    }
}

app.shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './components/app/app.component';
import { HomeComponent } from './components/home/home.component';
import { LoginComponent } from './components/login/login.component';
import { MobileComponent } from './components/mobile/mobile.component';

@NgModule({
    declarations: [
        AppComponent,
        HomeComponent,
        LoginComponent,
        MobileComponent
    ],
    imports: [
        CommonModule,
        HttpModule,
        FormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'login', component: LoginComponent },
            { path: 'mobile', component: MobileComponent },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModuleShared {
}

我,不知道我在哪里,做错了。因为我,是 Angular 新手。我试图在 @NgModule 下添加 HttpClient 但给出了一些其他错误。因为据我所知,我不需要添加 app.shared.module.ts 文件。由于 HttpClient 用于服务和组件级别。

谁能告诉我我在哪里做错了。

最佳答案

HttpClient模块需求HttpClientModule而不是 HttpModule要导入并添加到模块的导入中。

更多信息见 Documentation

import { HttpClientModule } from '@angular/common/http';

@NgModule({
    declarations: [
        ...
    ],
    imports: [
        ...
        HttpClientModule,
        ...
    ]
})
export class AppModuleShared { }

关于asp.net-mvc - NullInjectorError : No provider for HttpClient! Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562349/

相关文章:

asp.net - 是否可以在 Mac 上托管 ASP.NET Core 站点?

jquery - "A circular reference was detected while serializing an object of type ' 系统.Reflection.RuntimeModule '"

asp.net - 我应该继续在我的 MVC 1.0 项目上工作还是停下来学习 MVC 2.0?

css - 无法让背景图片在 stacklayout 上工作

javascript - 导出 `index.ts` 中的所有文件

typescript - 如何使用 TypeScript 动态调用对象的方法?

asp.net-mvc - MVC : used partial view for jquery modal popup, 验证问题

javascript - Ionic 3 WP-REST API 发布请求 401(未经授权)错误

javascript - 无法使用 Nest CLI 创建新项目

angular - ionic 3 angular 4 中的 WebRTC cordova 插件