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