Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Error: No provider for ObservableMedia

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

like image 860
Umer Farooqui Avatar asked Sep 23 '17 01:09

Umer Farooqui


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"

like image 182
Marina Gulakova Avatar answered Nov 20 '22 08:11

Marina Gulakova


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
like image 35
DMayes Avatar answered Nov 20 '22 08:11

DMayes


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) {}
like image 1
Khaled Lela Avatar answered Nov 20 '22 07:11

Khaled Lela