typescript - Why use a Service and Token / OpaqueToken to get the Window object in Angular -
i speaking colleague other day didn't code. in angular component access window
object private mywindow = window;
he told me better way use service , token / opaquetoken (i have no idea opaquetoken is), so...
import { injectable, opaquetoken, } '@angular/core'; export const windowtoken = new opaquetoken('app.window'); @injectable() export class windowservice { getwindow(): window { return window; } }
and can inject service , object so...
mywindow: window; constructor(@inject(windowtoken) windowref: windowservice) { this.mywindow = windowref.getwindow(); }
it seems nice , clean don't understand advantage of using token (or token does) , service on assigning variable window
object - can tell me why using service , token better practice?
Comments
Post a Comment