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

前端 未结 2 1251
情深已故
情深已故 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;
         }
    
    0 讨论(0)
  • 2021-02-12 15:23

    If you want to use this inside the callback, use an arrow function, otherwise it won't work

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

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    0 讨论(0)
提交回复
热议问题