请帮助..我被角HTTP困在这里
我试图
在我运行的json服务器上使用cmd从JSON服务器获取JSON数据和图片(json-server --watch db.json)......然后= =>
我的服务器结构
json-server
|
|__ public (folder for assets)
| |__ images (folder for images)
|
|__ db.json (json database)
我有以下错误!!?
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[baseURL -> baseURL -> baseURL]:
NullInjectorError: No provider for baseURL!
get@http://localhost:4200/vendor.js:46512:27
... etc
菜服务.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { baseURL } from '../shared/baseurl';
import { map } from 'rxjs/operators';
import { Dish } from '../shared/dish';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DishService {
constructor(private http: HttpClient) { }
getDishes(): Observable<Dish[]> {
return this.http.get<Dish[]>(baseURL + 'dishes');
}
}
disc.ts(只是从 json 服务器获取数据的类)
import { Comment } from './comment';
export class Dish {
id: string;
name: string;
image: string;
category: string;
featured: boolean;
label: string;
price: string;
description: string;
comments: Comment[];
}
共享文件夹 > baseurl.ts
export const baseURL = 'http://localhost:3000/'; // json server URL
menu.component.ts
import { Component, OnInit, Inject } from '@angular/core';
import { Dish } from '../shared/dish';
import { DishService } from '../services/dish.service';
...
export class MenuComponent implements OnInit {
dishes: Dish[];
constructor(private dishService: DishService,
@Inject('baseURL') private baseURL: string) { }
ngOnInit() {
this.dishService.getDishes().subscribe(dishes => this.dishes = dishes);
}
menu.component.html
<div fxFlex *ngIf="dishes">
<mat-grid-list cols="2" rowHeight="200px">
<mat-grid-tile *ngFor="let dish of dishes" [routerLink]="['/dishdetail', dish.id]">
<img height="200px" src="{{ baseURL + dish.image }}" alt={{dish.name}}>
<mat-grid-tile-footer>
<h1>{{dish.name | uppercase}}</h1>
</mat-grid-tile-footer>
</mat-grid-tile>
</mat-grid-list>
</div>
app.module.ts
import { HttpClientModule } from '@angular/common/http';
import { baseURL } from './shared/baseurl';
...
imports { HttpClientModule },
providers: [
DishService,
{provide: 'BaseURL', useValue: baseURL}
]