typescript - 角度 6 : Why does HttpClient work even without importing HttpClientModule?

标签 typescript angular6 angular-httpclient angular-module

我通过运行 ng new first-project 安装了一个准系统 angular 6 项目,我得到了一个默认的应用程序模块和组件。现在,我通过运行 ng generate module viewng generate component view/view 创建了一个新模块和其中的一个组件。 现在我的 app.module.ts 看起来像这样

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TestService } from './test.service';
import { ViewModule } from './view/view.module';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        FormsModule,
        HttpClientModule,
        ViewModule
    ],
    providers: [
        TestService
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

view.module.ts

import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { ViewComponent } from './view/view.component';

@NgModule({
    declarations: [ViewComponent],
    imports: [
        CommonModule,
        FormsModule
    ],
    exports: [
        ViewComponent
    ]
})
export class ViewModule { }

view.component.ts

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

@Component({
  selector: 'app-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.css']
})
export class ViewComponent implements OnInit {

    username: string = "";
    response: any;

    constructor (private http: HttpClient) { }

    search () {
        this.http.get('https://api.github.com/users/' + this.username)
        .subscribe((response) => {
            this.response = response;
            console.log(this.response);
        });
    }

}

所以问题是,我没有在 view.module.ts 中导入 HttpClientModule,而是在 app.module.ts 中导入,我直接导入了 HttpClient在 view.component.ts 里面,我什至成功地得到了回应。 为了测试这种情况是否经常发生,我从 view.module.ts 中删除了 FormsModule 导入,以查看我是否在 view.component 中使用了 [(ngModel)]。 html 仍然有效,因为我已经在 app.module.ts 中导入了 FormsModule,但它没有。

我的问题是,为什么 HttpClientModule 让我导入它并在不同的模块中使用它,为什么其他模块不能那样工作? 我试图在网上找到原因,但没有找到。谁能告诉我原因吗?谢谢。

附加信息: 1. 如果我不在任何地方导入 HttpClientModuleHttpClient 将不起作用。 所以模块需要在项目的某个地方导入。 2. 它也以另一种方式工作,即通过在 view.module.ts 中导入模块并在 app.component.ts 中使用它。这意味着,只需导入一次模块,我就可以在整个项目中使用 HttpClient

最佳答案

根据official document对于 Angular,不需要在 view.module.ts 中导入 HttpClientModule,只要在 AppModule.ts 中导入它即可.

以下是该网站的引述:

Before you can use the HttpClient, you need to import the Angular HttpClientModule. Most apps do so in the root AppModule. Having imported HttpClientModule into the AppModule, you can inject the HttpClient into an application class as shown in the following ConfigService example.

关于typescript - 角度 6 : Why does HttpClient work even without importing HttpClientModule?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388352/

相关文章:

javascript - 如何在 hbs 模板中显示数据?

javascript - 表格数组 : Cannot find control with unspecified name attribute in angular6

javascript - 将 ts 用于 tsx react 文件

html - 如何根据 Angular 6 中的条件禁用复选框?

angular - 将焦点更改为以 Angular 6 提交的表单

angular - 使用自定义配置运行 ng build

angular - 无法在 POST url Angular 传递参数

coffeescript - 将 Typescript 添加到 Coffeescript

Angular 6 : Calling service observer. 接下来来自 Http 拦截器导致无限请求循环