首页 文章

angular 2 http得到404

提问于
浏览
2

我正在尝试复制ngPrime数据表演示https://github.com/primefaces/primeng . 我试图弄乱路径,但我无法弄清楚这一点 . 我继续得到404 .

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Car} from './car';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class CarService {

constructor(private http: Http) {}

getCarsSmall() {
    return this.http.get('./cars-small.json')
                .toPromise()
                .then(res => <Car[]> res.json().data)
                .then(data => { return data; });
 }
}

这是他们网站的src . 我必须导入rxjs toPromise并修改角度核心包定义 .

import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Car} from '../domain/car';

@Injectable()
export class CarService {

constructor(private http: Http) {}

getCarsSmall() {
    return this.http.get('/showcase/resources/data/cars-small.json')
                .toPromise()
                .then(res => <Car[]> res.json().data)
                .then(data => { return data; });
 }
}

使用完整路径解决了问题:

return this.http.get('/src/app/cars-small.json')

2 回答

  • 1

    由于某些原因,这项改变工作 -

    return this.http.get('/src/app/cars-small.json')
    

    当文件处于同一级别时,我真的不明白为什么我必须上两个目录 . 我试过app / cars-small.json,但这也没用 .

  • 0

    也许您应该将文件夹添加到webpack包中作为资产目录 . 然后你可以要求 http.get('api/config.json')...

    "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "api",
        "assets",
        "favicon.ico"
      ],....
    

相关问题