【问题标题】:Error while Reading disk JSON data: 404 error [duplicate]读取磁盘 JSON 数据时出错:404 错误 [重复]
【发布时间】:2021-10-22 15:40:23
【问题描述】:

我尝试使用服务从磁盘读取 JSON 数据:

import { Product } from './../models/Product';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

type ProductRecord = Record<number, { product: Product; quantity: number }>;

@Injectable({
    providedIn: 'root'
})
export class ProductsService {
    productRecords: ProductRecord[] = [];
    currentPrduct: Product = {} as Product;

    constructor(private httpClient: HttpClient) {}

    public getProducts(): Observable<Product[]> {
        const url: string = 'src/assets/data.json';
        return this.httpClient.get<Product[]>(url);
    }

}

提供了文件夹结构:

我收到下面提供的错误:

GET http://localhost:4200/src/assets/data.json 404 (Not Found)

显然数据在源文件夹中并且位置正确,我收到 404 错误的原因是什么?

谢谢。

【问题讨论】:

  • 从网址中删除src
  • 谢谢,删除src后这对我有用

标签: angular typescript


【解决方案1】:

从 URL 中删除 src。

当项目编译时,assets文件夹进入根文件。

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多