Tuesday, 19 November 2019

What's the difference between providing and injecting 'Window' vs Window in Angular 8 and 9?

I have two Angular projects using these versions:

  • 9.0.0-next.6
  • 8.1.0

In the version 9 I used this to provide and inject the window obhject:

@NgModule({
  providers: [
    {
      provide: Window,
      useValue: window
    },
  ]
})

export class TestComponent implements OnInit {
  constructor(@Inject(Window) private window: Window)
}

Which works fine.


Taking this approach to version 8 throwed warnings and errors during compilation:

Warning: Can't resolve all parameters for TestComponent …

I solved it by using single quotes, like this:

@NgModule({
  providers: [
    {
      provide: 'Window',
      useValue: window
    },
  ]
})

export class TestComponent implements OnInit {
  constructor(@Inject('Window') private window: Window)
}

What is the difference between both version?
What is the difference in Angular 8 and 9 that causes this thing?



from What's the difference between providing and injecting 'Window' vs Window in Angular 8 and 9?

No comments:

Post a Comment