1468
برای اینکه بتونم اطلاعات رو در پروژه انگولار از بانک اطلاعاتی بخونم یه service اضافه کردم و برای استفاده لازم بود این خط کد رو import کنم
import {HttpClientModule, HttpClient } from '@angular/common/http';
و ردر service هم همینطور
import { HttpHeaders, HttpClient } from '@angular/common/http';
اما زمانی پروژه رو اجرا کردم به ا ین خطا برخوردم
This likely means that the library (@angular/common/http) which declares HttpClient has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking
with the library's authors to see if the library is expected to be compatible with Ivy.
برای رفع این خطا ا HttpClient رو از app.model.ts حذف کردم و فقط همونHttpClientModul را نگه داشتم و به جای آن HttpClient را در News.Service.ts اضافه کردم
کل پروسه این شکلی شد
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [
AppComponent,
HomeNewsComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
و در service
import { Injectable } from '@angular/core';
import {NewsModel} from '../Models/News.Model';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
const httpOption = {headers: new HttpHeaders({'content-Type': 'application/json'})};
const newsUrl = 'https://localhost:5001/api/News';
@Injectable({
providedIn: 'root'
})
export class NewsService {
selectedNews: NewsModel;
NewsList: NewsModel[];
constructor(private http: HttpClient) { }
getNewsList(): Observable<NewsModel[] > {
return this.http.get<NewsModel[]>(newsUrl);
}
}