【问题标题】:Polymer Rest Api not working聚合物休息 API 不工作
【发布时间】:2017-08-13 12:58:09
【问题描述】:

我正在使用 Web 组件 Iron ajax 按照本教程进行 REST api 调用http://frontendinsights.com/polymer-rest-api-using-iron-ajax/ 我正在获取这些数据并使用 predix 数据表进行存储,以使用数据表https://www.predix-ui.com/#/components/px-data-table/ 在 ui 上显示从 api 获取的数据,该数据表基本上接受 JSON 格式的数据。

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-
 ajax.html">
<link rel="import" href="../../bower_components/px-data-table/px-data-
 table.html">
 <dom-module id="show-repositories">
 <template>

  <px-data-table
  table-data="{{githubrepository}}"
  language="en"
  sortable>
  <px-data-table-column name="repository"></px-data-table-column>
  <px-data-table-column name="id" label="id"></px-data-table-column>
  </px-data-table>

    Repositories:
    <span>{{githubrepository}}</span>
    <span>{{repos}}</span>

    <iron-ajax
        id="requestRepos"
        url="https://api.github.com/users/burczu/repos"
        params='{"type":"all"}'
        handle-as="json"
        on-response="handleResponse">
    </iron-ajax>
</template>

<script>
    Polymer({
        is: 'show-repositories',
        properties: {
            repos: {
                type: Array
            },
            githubrepository:{
              type: Array
            }

        },
        ready: function () {
            this.$.requestRepos.generateRequest();

        },
        handleResponse: function (data) {
             this.repos = data.detail.response;

            for (var i = 0; i < this.repos.length; i++) {
              this.githubrepository[i] = {"id":this.repos[i].id,"name":this.repos[i].name}
            }

            console.log(this.repos);
           console.log(this.githubrepository);
        }


    });
</script>

两个控制台日志都显示数据是 json 格式,但是当我使用 {{repos}} 时,它显示其中包含的数据,但是当我使用 {{githubrepository}} 时,它不显示数据,即使我无法使用 prredix webcomponent 打印数据。 我无法弄清楚这里出了什么问题?

【问题讨论】:

    标签: javascript polymer polymer-1.0


    【解决方案1】:

    改变你的 js。

    handleResponse: function (data) {
        this.repos = data.detail.response;
    
        var githubrepository = [];
        for (var i = 0; i < this.repos.length; i++) {
            githubrepository[i] = {"id":this.repos[i].id,"name":this.repos[i].name}
        }
        this.githubrepository = githubrepository;
    
        console.log(this.repos);
        console.log(this.githubrepository);
    }
    

    【讨论】:

    • this.githubrepository 也应该可以在函数外访问?
    猜你喜欢
    • 2015-08-19
    • 2014-03-15
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多