找不到Angular2服务调用json路径

时间:2017-09-14 07:46:06

标签: angular2-services

product.service.ts文件:

import { Injectable } from '@angular/core';
import { Http , Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import { IProduct } from './product';

@Injectable()
export class ProductService {
    private _producturl='src/products.json';
    constructor(private _http: Http){}

   getproducts(): Observable<IProduct[]> {
   return this._http.get(this._producturl)
  .map((response: Response) => <IProduct[]> response.json())
  .do(data => console.log(JSON.stringify(data)));
   }
  }

请在下面找到文件夹结构的附件截图

但是当页面加载时它会显示一个错误: zone.js:2744获取http://0.0.0.0:4200/src/products.json 404(未找到)

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要将export class ProductService { private _producturl='./assets/products.json'; constructor(private _http: Http){} ......... } 文件保存在资源文件夹中,然后从此处进行访问

SELECT 
    BDETAIL.HSN, 
    SUM(BDETAIL.TAXABLE), 
    SUM(BDETAIL.SGST_V), 
    SUM(BDETAIL.CGST_V), 
    SUM(BDETAIL.TOTAL), 
    BDETAIL.SGST_P 
FROM 
    BDETAIL 
JOIN 
    BILL ON BDETAIL.BILL_ID = BILL.BILL_ID 
WHERE 
    BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY 
    BDETAIL.HSN, BDETAIL.SGST_P 

如果您使用Angular Cli,则需要按照官方文档将json文件保存在Assets文件夹(与app dir并行)目录中。