Angular Error: No provider for ObservableMedia

為{幸葍}努か 提交于 2019-12-10 14:09:08

问题


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


回答1:


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"




回答2:


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


来源:https://stackoverflow.com/questions/46375067/angular-error-no-provider-for-observablemedia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!