简体   繁体   English

错误TS2339:JavaScript到Typescript错误

[英]error TS2339: JavaScript to Typescript error


ERROR in src/app/dashboard.component.ts(49,15): error TS2339: Property 'style' does not exist on type 'Element'. src / app / dashboard.component.ts(49,15)中的错误:错误TS2339:“元素”类型上不存在属性“样式”。 src/app/dashboard.component.ts(50,16): error TS2339: Property 'textContent' does not exist on type 'EventTarget'. src / app / dashboard.component.ts(50,16):错误TS2339:属性'textContent'在类型'EventTarget'上不存在。


This works as a javascript function, but the above are the errors I get when I try to use this in typescript. 这作为一个javascript函数,但上面是我尝试在打字稿中使用它时得到的错误。 I have tried changing var to let and adding . 我已经尝试更改var以允许和添加。 any other suggestions? 还有其他建议吗? Thanks. 谢谢。

dashboard.ts dashboard.ts

var w = document.getElementById('wrapper');
var button = document.getElementById('randomize');
var image = w.children; // inner elements, your quotes divs

// a function to hide all divs
var hideDivs = function(divs) {
  for (var div of divs) {
    div.style.display = 'none';
  }
}

hideDivs(image); // hide all initially

// on click
button.addEventListener('click', function(event) {
  var rnd = Math.floor(Math.random() * image.length); // get random index
  hideDivs(image); // hide all quotes
  image[rnd].style.display = 'block'; // show random quote
  event.target.textContent = 'Click one more time!'; // set button text. event.target is the button you've clicked
})

dashboard.html dashboard.html

  <div id="wrapper">
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
</div>
<button id='randomize'>Next</button>

The problem is that target is typed as EventTarget , and an element of image which is a HTMLCollection is typed as Element . 问题是target被输入为EventTarget ,而作为HTMLCollectionimage元素被输入为Element Both of the properties you want to access are defined on HTMLElement . 您要访问的两个属性都在HTMLElement上定义。 The simplest thing you can do is to use a type assertion to tell the compiler that in your case both of these are actually HTMLElement 你能做的最简单的事情是使用一个类型断言来告诉编译器在你的情况下这两个都是HTMLElement

// on click
button.addEventListener('click', function (event) {
    var rnd = Math.floor(Math.random() * image.length); // get random index
    hideDivs(image); // hide all quotes

    (image[rnd] as HTMLElement).style.display = 'block'; // show random quote
    (event.target as HTMLElement).textContent = 'Click one more time!'; // set button tqext. event.target is the button you've clicked
})

Yo might also want to type hideDivs correctly as well : Yo也可能也想正确输入hideDivs

// a function to hide all divs
const hideDivs = function (divs : HTMLCollection) {
    for (var div of divs) {
        (div as HTMLElement).style.display = 'none';
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 无法编译类型不存在的错误 TS2339 - Typescript can't compile type not existing Error TS2339 为什么在键入Redux-saga调用参数时Typescript会引发错误-TS2339 - Why does Typescript throw an error when typing Redux-saga call parameter - TS2339 打字稿错误TS2339:'Window'类型中不存在属性'webkitURL' - Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window' TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 React + TypeScript:将React组件作为道具传递,使用道具进行渲染[错误:TS2339] - React + TypeScript: Passing React Component as Props, rendering with props [Error: TS2339] Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' TypeScript 类型与 JSX.Element 反应将不接受.map() 并给出错误 TS2339 - TypeScript types with JSX.Element on react will not accept .map()and gives error TS2339 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.**
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM