首页 > 解决方案 > 未找到 Angular Http 方法

问题描述

我已将此作为我的一个项目的核心项目,并且我坚持使用它。
我尝试使用 http 方法,但失败了。我尝试将 HttpClientModule 添加到 app.module 但仍然没有。

我得到的错误是:

app.ae014c5e7b696f87de83.bundle.js:107 ERROR TypeError: 
Cannot read property 'method' of undefined



All I did in the app.component.ts file add




  export class AppComponent extends BaseComponent implements OnInit {
  constructor(
      private readonly i18nStore: Store<I18NState>,
      private readonly config: ConfigService,
      private http: HttpClient,
      @Inject(PLATFORM_ID) public platformId: Object
  ) {
    super();
    // TODO: ngx-i18n-router
    // private readonly i18nRouter: I18NRouterService) {
  }

  ngOnInit(): void {
    this.i18nStore.dispatch(new Init(this.config.getSettings('i18n')));
  }

  createUserA(): void  {
      this.http.get<any>('https://swapi.co/api/people/1')
          .subscribe(data => console.log('data', data));
  }
}

机器人获取和发布不起作用。我已经导入:

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

标签: httpangular6

解决方案


添加到您的app.component.ts

contructor(private http : HttpClient) {}

推荐阅读