【问题标题】:Binding and Displaying Data With Aurelia使用 Aurelia 绑定和显示数据
【发布时间】:2017-01-19 05:34:30
【问题描述】:

我是 Aurelia 的新手。我有一个 WebApi,它将返回一些我想填充到导出模型中的数据,然后在屏幕上显示信息。我认为它会进入我的运行事件,但不确定。谁能告诉我该怎么做。任何信息将不胜感激。我的代码如下。

--杰森

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

declare var window: { wcApiUrl: string, wcAmtInstanceId: string };

@inject(HttpClient)
export class BureauModUpdate {

    files: string;
    
    constructor(private http: HttpClient) {
        http.configure(x => {
            x.defaults.headers = { 'Authorization': 'Basic ' + window.wcAmtInstanceId }
        });
    }

    public run(): void {
        //Would I put it here ??

    }
     upload(): void {
         var form = new FormData()
         for (var i = 0; i <= this.files.length; i++) {
             form.append('file', this.files[i])
             this.http.fetch(window.wcApiUrl + '/Lookup/BureauModUpdate/CreateBureauModUpdates', {
                 method: 'post',
                 body: form
             })
         }

   

    }
}

export class BureauModUpdateHistory  {
    public IndexId: number;
    public UploadID:number;
    public EmployeeNum: number;
    public filename: string;
    public Bureau: string;
    public UploadedDate: Date;
    public UploadedStatus: string;
    public ErrorInfo: string;
    public RecordCount: number;
  
}

【问题讨论】:

标签: aurelia


【解决方案1】:

要在页面加载时发生某些事情,请使用attached() Aurelia 组件生命周期方法,如下所示:

attached() {
  // do something here
}

有关组件生命周期的更多信息,请参阅Aurelia's DocHub 上的文档。

使用 Fetch 获取数据的示例:

要使用 fetch(或任何其他 Web 服务)获取 HTTP 数据,您需要使用异步调用(使用 .then 链接下一个事件)。例如:

this.http.fetch(url).then(response => {
  this.data = response;
}

然后,只需将您的数据绑定到this.data(取决于您获取的数据类型)。例如:

<template>
  Hello, ${data.fname}!
</template>

【讨论】:

  • 上传工作正常。不是问题。我只是试图进行另一个 Webapi 调用来填充 BureauModUpdateHistory 类,然后在页面加载时显示信息。所以我认为它会像 run() 一样进行非常相似的 api 调用... public run(): void { this.http.fetch(window.wcApiUrl + '/Lookup/BureauModUpdate/GetLatestBureauModUpdate', { 方法:'get', })
  • 我的帖子的重点不是关于upload() 例程——它是向您展示页面加载时您想要运行的任何内容都应该放在attached() 方法中.我会更新我的答案以使其更清楚。
  • 好的,谢谢。现在敲门。所以现在我有一个类(BureauModUpdateHistory),我试图将响应绑定到该类。就像你用 this.data 展示的一样。但现在它告诉我“响应”类型不能分配给我的班级。我需要做什么?
  • 您从 http fetch 调用中收到的数据格式是什么?您可以在.then() 函数中添加console.log(response); 来检查它。
  • 你能得到这个工作吗?我的回答有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2011-09-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
相关资源
最近更新 更多