簡體   English   中英

錯誤:類型“字符串 []”不可分配給類型“字符串”

[英]Error: Type 'string[]' is not assignable to type 'string'

我有一個迷你項目,我需要制作一個帶有歷史的簡單計算器。 在此項目的一部分中,如果單擊其中一個歷史記錄項,則應將其刪除,但是在執行此操作時出現錯誤。 你有解決這個錯誤的方法和理論嗎? 關於我刪除您擁有的項目的解決方案

類型 'string[]' 不能分配給類型 'string'。

 let text: string = ""; const historyArrey: Array<String> = []; let equal = document.getElementsByClassName("btn-blue"); equal[0].addEventListener("click", () => { let response = eval(text); let historyBox = document.getElementById("history"); (<HTMLInputElement>document.getElementById('screen')).value = response; historyArrey.push(text + "=" + response); console.log(historyArrey); if (historyBox) { historyBox.innerHTML = historyArrey.map((i) => {//error return "<p onclick='removeItem()'>" + { i } + "</p>" }); historyArrey.join("<br/>") } })

問題

您在這里join

historyBox.innerHTML = historyArrey.map((i) => {
  return "<p onclick='removeItem()'>" + { i } + "</p>"
});
historyArrey.join("<br/>")

什么也沒做。

解決方案

您必須在分配之前join數組

historyBox.innerHTML = historyArrey.map((i) => {
  return "<p onclick='removeItem()'>" + { i } + "</p>"
}).join("<br/>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM