typescript中this报错
生活随笔
收集整理的這篇文章主要介紹了
typescript中this报错
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
export class AppComponent {title = 'myapp';count=1;clickme=function(){this.count++; }
在上述代碼中,使用this報錯:?'this' implicitly has type 'any' because it does not have a type annotation.
function處報錯:?An outer value of 'this' is shadowed by this container
?
出錯原因:ts?提供類似C# 和 java的靜態類型(強類型), 在全局和命名空間的全局里面 直接聲明一個函數要用到 function 關鍵字(就是js的function關鍵字),
而在類(class)里面卻不能使用function來聲明方法。
?
這其中是this的指向問題.
改成這樣就可以了:
export class AppComponent {title = 'myapp';count=1;clickme=()=>{this.count++;} }?
總結
以上是生活随笔為你收集整理的typescript中this报错的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: delphi oracle 分页,使用原
- 下一篇: 安防网络摄像机推送到RTMP流媒体服务器