6

我正在做一个 POC 来证明我在 Angular Universal 的后端和前端之间进行了通信。我在后端有一个名为 heros.json 的 JSON 文件,我想ModelServicemodel.service.ts.

我有这个文件夹结构:

在此处输入图像描述

model.service.ts(前端)内,我想创建一个 http 请求以在名为getStuff().

我在model.service.ts中有这个:

// domain/feature service
@Injectable()
export class ModelService {
  private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
   // This is only one example of one Model depending on your domain
  constructor(public api: ApiService, public cacheService: CacheService, private http: Http) {

  }

  public getStuff(): Observable<any[]> {
        return this.http.get(this.heroesUrl)
                    .map(this.extractData)
                    .catch(this.handleError);
  }

    private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
  }

    private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || "";
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

    // domain/feature service
    @Injectable()
    export class ModelService {
      private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
       // This is only one example of one Model depending on your domain
      constructor(public api: ApiService, public cacheService: CacheService, private http: Http) {

      }

      public getStuff(): Observable<any[]> {
            return this.http.get(this.heroesUrl)
                        .map(this.extractData)
                        .catch(this.handleError);
      }

        private extractData(res: Response) {
        let body = res.json();
        return body.data || { };
      }

        private handleError (error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
          const body = error.json() || "";
          const err = body.error || JSON.stringify(body);
          errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
        } else {
          errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
      }

我从前端组件调用 ModelService.getHeroes:

export class HomeComponent {

      public data: any = {};
      constructor(public modelService: ModelService) {
        // we need the data synchronously for the client to set the server response
        // we create another method so we have more control for testing
        this.universalInit();
      }

      public universalInit() {

        this.modelService.getStuff().subscribe((data) => {
          this.data = data;
        });
      }

我收到此错误:

GET /src/backend/heroes.json 404 3.698 ms - 46
404 -  {"status":404,"message":"No Content"}
EXCEPTION: 404 -  {"status":404,"message":"No Content"}

/private/var/root/vepo/node_modules/rxjs/Subscriber.js:227
            throw err;
            ^
404 -  {"status":404,"message":"No Content"}
[nodemon] app crashed - waiting for file changes before starting...

所以我在服务中的网址private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file是错误的。鉴于该文件夹结构,网址是什么?因为实际运行的项目,输出,在 dist 中:

在此处输入图像描述

所以我不知道该放什么ModelService.heroesUrl。应该有什么字符串值ModelService.heroesUrl

4

3 回答 3

8

我将相同的文件places.json放到"assets"文件夹中,然后将 url 设置为:

places = this.http.request("http://localhost:4200/assets/places.json")

希望此信息对某人有所帮助。

于 2017-07-05T21:51:50.230 回答
4

您必须将您的 json 文件放入您的 dist 文件夹客户端,并且您必须将您的 url 更改为http://localhost:4000/dist/heroes.json<-- destination where you are putting your json file in dist directory

于 2016-12-21T05:25:08.890 回答
0

将包含模拟数据的目录添加到 angular-cli.json 中的“资产”部分。

于 2018-06-08T11:49:24.840 回答