无法通过在 Aurelia 中的 HttpClient 实例上调用 fetch 方法来执行 GET 请求

Not able to perform GET request by calling the fetch method on an instance of HttpClient in Aurelia

我正在尝试通过在 Aurelia 中的 HttpClient 实例上调用 fetch 方法来获取请求。

app.html

<template>
   <button click.delegate = "getData()">GET</button>  
</template>

app.js

import {HttpClient} from 'aurelia-fetch-client';

let client = new HttpClient();

export class App {

   getData() {
      httpClient.fetch('http://jsonplaceholder.typicode.com/posts/1')
      .then(response => response.json())
      .then(data => {
         console.log(data);
      });
   }
 }

main.js

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging()
    .plugin('aurelia-history-browser');


  aurelia.start().then(() => aurelia.setRoot());
}

我也安装了fetch bower install fetch

现在 运行 使用 http-server -o -c-1 的应用程序时,我遇到了以下错误

"GET /aurelia-history-browser" Error (404): "Not found"

我缺少什么。

更新

我终于做到了,但没有魔法(错过导入 'fetch';)它不在 official document

import 'fetch';
import {HttpClient, json} from 'aurelia-fetch-client';

let httpClient = new HttpClient();


export class App {

    constructor() { 

    this.UserRecords = this.fetchUserDataFromWebService();

    } 

    fetchUserDataFromWebService()
    {
          var serverData = "";


         httpClient.fetch('http://jsonplaceholder.typicode.com/posts')
              .then(response => response.json())
              .then(data => {               
                 serverData = data;             
              });

              alert('Data Received from Server');         
          return serverData;
    }
}

只有一个问题:如果我不使用 警告框 ,那么我会收到错误消息 "Error: Value for 'UserRecords' is non-repeatable" .如何解决?

终于我做到了..但没有魔法(错过导入 'fetch';)它不在 official document

import 'fetch';
import {HttpClient, json} from 'aurelia-fetch-client';

let httpClient = new HttpClient();


export class App {

    constructor() { 

    this.UserRecords = this.fetchUserDataFromWebService();//.slice(0,5);

    } 

    fetchUserDataFromWebService()
    {
          var serverData = "";


         httpClient.fetch('http://jsonplaceholder.typicode.com/posts')
              .then(response => response.json())
              .then(data => {               
                 serverData = data;             
              });

              alert('Data Received from Server');         
          return serverData;
    }
}

只有一个问题...如果我不使用警告框,那么我会收到错误"Error: Value for 'UserRecords' is non-repeatable"。如何解决?

回答你答案中的问题,试试这个:

export class App {

    constructor() { 

    this.UserRecords = null;//.slice(0,5);
   this.fetchUserDataFromWebService();
    } 

    fetchUserDataFromWebService()
    {
         httpClient.fetch('http://jsonplaceholder.typicode.com/posts')
              .then(response => response.json())
              .then(data => {               
                 this.UserRecords = data;             
              });

    }
}