Angular2 + Typescript + FileReader.onLoad = property does not exist

前端 未结 2 1253
情深已故
情深已故 2021-02-12 14:52

I am using the FileReader Interface and it’s asynchronous method readAsText() to read a local text file, After that when the onload ev

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 15:02

    You are using a regular javascript function here:

     reader.onload = function(e) {
            this.text=reader.result;
         }
    

    The this belongs to the function not your class.

    Use arrow function

    reader.onload = (e)=> {
                this.text=reader.result;
             }
    

    Or

    self = this;
     reader.onload = function(e) {
            self.text=reader.result;
         }
    

提交回复
热议问题