简体   繁体   English

我的 Angular 应用程序在 app.component.ts 文件中出现错误

[英]My Angular App Has An Error In app.component.ts File

I have finished my Angular app which is connected to my API, but there is one problem I can't seem solve, HttpClient won't import because it has 'no exported' member.我已经完成了连接到我的 API 的 Angular 应用程序,但有一个我似乎无法解决的问题,HttpClient 不会导入,因为它没有“无导出”成员。 I have tried various things and I tried to export the httpclient in the http file.我尝试了各种方法,并尝试在 http 文件中导出 httpclient。

Here is my app.component.ts file这是我的 app.component.ts 文件

import { HttpClient } from './services/http.service';
import { Snake } from './_interfaces/snake.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public snake: Snake[];

  constructor(private http: HttpClient){}

  public getSnake = () => { 
    let route: string = 'http://localhost:5000/api/snake';
    this.http.getData(route) 
   .subscribe((result) => {
      this.snake = result as Snake[];
    },
    (error) => {
      console.error(error);
    });
  }
 }

Here is my App Module file这是我的应用程序模块文件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpClient } from '@angular/common/http'; 
import  {httpService} from './http.service'

import { AppComponent } from './app.component';

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

Here is my http service file这是我的 http 服务文件

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';


@Injectable()
export class httpService
 {

  constructor(private http: HttpClient) { 

  }

  public getRoute():Observable<any>{
    return this.http.get('url');
  }

 }

I'm not too sure about the syntax or how to make sure that the app component file can import http, this is my first Angular app.我不太确定语法或如何确保应用程序组件文件可以导入 http,这是我的第一个 Angular 应用程序。

Please update update your service to look something like this.请更新更新您的服务看起来像这样。

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

    @Injectable()
    export class httpService
     {

      constructor(private http: HttpClient) { 

      }

      public getRoute():Observable<any>{
        return this.http.get('url');
      }

     }

Then import your service in your app module and include in your providers after that try accessing in component file然后在您的应用程序模块中导入您的服务并包含在您的提供者中,然后尝试访问组件文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM