元素隐式地具有“ any”类型,因为类型“ Response”没有索引签名。打字稿中的错误

时间:2018-06-28 12:50:13

标签: json typescript

我想用api调用填充数据数组。 api响应将为json类型。当我尝试从api获取数据时,出现错误“元素隐式具有'any'类型,因为类型'Icons'没有索引签名。”

modelsearch.component.ts

Thanks for your effort guys, I fixed it with adding id to each image, I know it's hard coded... But oh well, I will get better.



 // Loop through images
       for(i=0; i < img.length; i++){
          img[i].onclick = function(){
            modal.style.display = "block";
            modalImg.src = this.src;
            modalName.innerHTML = myName[this.id-1].innerHTML;
            modalDesc.innerHTML = myDesc[this.id-1].innerHTML;
          }
        }



          <div>
            <img class="myImg" src="images/2.png" alt="" id="1">
            <h5 class="myName">Image name1</h5>
            <p class="myDesc">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis, quaerat, aut.</p>
          </div>
          <div>
            <img class="myImg" src="images/3.png" alt="" id="2">
            <h5 class="myName">Image name2</h5>
            <p class="myDesc">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis, quaerat, aut.</p>
          </div>
          <div>
            <img class="myImg" src="images/4.png" alt="" id="3">
            <h5 class="myName">Image name3</h5>
            <p class="myDesc">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laudantium, debitis, provident.</p>
          </div>
api的

json响应:

    public data = [];
    constructor(private apiService: ApiService) {
        this.apiService.getModels().subscribe(response => {
            console.log(response);
            if (response['status'] == 200) {
                this.data = response['_body'];

            } else if (response['status'] == 500) {
                console.log("error");
            }
        });

    }

0 个答案:

没有答案