我是聚合物新手,我正在尝试读取自定义元素中的JSON数据并将其显示在其他元素中。使用聚合物阅读和显示json数据
这是我的JSON数据:
jsonData.json
[
{
"name":"Ladies+Chrome+T-Shirt",
"title":"Ladies Chrome T-Shirt"
},
{
"name":"Ladies+Google+New+York+T-Shirt",
"title":"Ladies Google New York T-Shirt"
}
]
这是我shop-app.html
文件,其中我尝试读取JSON文件中的数据(我不知道这是否正确或不作为我无法测试它):
<dom-module id="shop-category-data">
<script>
(function(){
class ShopCategoryData extends Polymer.Element {
static get is() { return 'shop-category-data'; }
static get properties() { return {
data: {
type: Object,
computed: '_computeData()',
notify: true
}
}}
_computeData() {
this._getResource({
url: 'data/jsonData.json',
onLoad(e){
this.set('data.items', JSON.parse(e.target.responseText));
}
})
}
_getResource(rq) {
let xhr = new XMLHttpRequest();
xhr.addEventListener('load', rq.onLoad.bind(this));
xhr.open('GET', rq.url);xhr.send();
}
}
customElements.define(ShopCategoryData.is, ShopCategoryData);
})();
</script>
</dom-module>
这是我想要显示数据的元素I rea从JSON文件d:
<dom-module id="shop-app">
<template>
<app-location route="{{route}}"></app-location>
<app-route
route="{{route}}"
pattern="/:page"
data="{{routeData}}"
tail="{{subroute}}">
</app-route>
<shop-category-data data="{{data}}"></shop-category-data>
<template>
<div> Employee list: </div>
<template is="dom-repeat" items="{{data}}">
<div>First name: <span>{{item.name}}</span></div>
<div>Last name: <span>{{item.title}}</span></div>
</template>
</template>
</template>
<script>
class ShopApp extends Polymer.Element {
static get is() { return 'shop-app'; }
}
customElements.define(ShopApp.is, ShopApp);
</script>
</dom-module>
线<shop-category-data data="{{data}}"></shop-category-data>
应该给我的数据,我然后尝试使用DOM重复显示。但没有任何显示。所以,我认为在阅读JSON数据时存在一些错误。
编辑:
的JSON被正确读取,它只是没有得到在反射回我:
<shop-category-data data="{{data}}"></shop-category-data>
您是否首先获得了JSON数据? –
你用JSON.stringify解析了JSON数据吗? –
您是否尝试过使用ready:function(){}来获取JSON而不是计算机()? –