Angular 2 window.postmessage

后端 未结 3 2157
时光取名叫无心
时光取名叫无心 2020-12-30 06:01

I\'m working on an eCommerce application in Angular 2 with a node and express api backend. I\'m currently working on the social login section where I am using passport-faceb

3条回答
  •  遥遥无期
    2020-12-30 06:53

    You should change the way you declare the eventListener (by using bind(this) or an anonymous function, or change the way you declare your eventHandlers:

    bind(this):

    constructor(private _authService: AuthService, private _router: Router) { 
       if (window.addEventListener) {
         window.addEventListener("message", this.receiveMessage.bind(this), false);
       } else {
          (window).attachEvent("onmessage", this.receiveMessage.bind(this));
       }
    }
    

    anonymous function

    window.addEventListener("message", () => {
       this.receiveMessage();
    }, false)
    

    different declaration of eventHandler

    receiveMessage: any = (event: any) =>  {
      //...
    }
    

    Take your pick :) With the last option it is easy to detach the eventListener

提交回复
热议问题