Angular Error: No provider for ObservableMedia

Viewed 6944

I am trying to user ObservableMedia. Application compiles successfully but in browser console i see this error

ERROR Error: No provider for ObservableMedia! at injectionError (core.es5.js:1169) at noProviderError (core.es5.js:1207)

Here is my my code

import { Component , OnInit, ViewChild} from '@angular/core';    
    import {Observable} from 'rxjs/Rx';
    import {MediaChange, ObservableMedia} from "@angular/flex-layout";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],

})
export class AppComponent implements OnInit {


constructor(
  public media: ObservableMedia) {

 }

  routeLinkClick() {
    if (!this.media.isActive('gt-xs')) {
      this.sidenav.toggle();
    }
  }

Please help. thanks

3 Answers

In flex-layout versions 7.0.0-beta.20 and higher ObservableMedia was replaced with MediaObserver.

media-observer: ObservableMedia is now deprecated in anticipation of RxJS v7. The new API is called MediaObserver, and provides the exact same functionality as ObservableMedia, except you cannot directly subscribe to it,

Developers should subscribe to MediaObserver's media$ property; in place of subscribing directly to ObservableMedia. - quoted from https://github.com/angular/flex-layout/blob/master/CHANGELOG.md

After I made that change, it worked with no issues on the latest versions as of today:

"@angular/core": "^7.1.1",
"@angular/flex-layout": "^7.0.0-beta.23",
"rxjs": "^6.3.3",
"rxjs-compat": "^6.3.3"

Try adding this to your imports at the top of your app.module.ts file:

import { FlexLayoutModule } from '@angular/flex-layout';

And add this into your imports array within the @NgModule of your app.module.ts file:

FlexLayoutModule

ObservableMedia replaced by MediaObserver from @angular/flex-layout v7.0.0-beta.24 (2019-03-17)

Before:

import {MediaChange, ObservableMedia} from "@angular/flex-layout"; constructor(public media: ObservableMedia) {}

After:

import {MediaChange, MediaObserver} from "@angular/flex-layout";
constructor(public media: MediaObserver) {}
Related