components icon indicating copy to clipboard operation
components copied to clipboard

No provider for Overlay! StaticInjectorError(AppModule)[MatMenuTrigger -> Overlay]:

Open elvisbegovic opened this issue 8 years ago • 44 comments

Bug, feature request, or proposal:

bug

What are the steps to reproduce?

I update from angular 5.x and cli 1.7.x and rxjs 5.x to v6rc.x I only update in my module this MatMenuModule and use it like:

<div *ngFor="let item of data">
  <button mat-icon-button [matMenuTriggerFor]="appMenu"></button>
  <mat-menu #appMenu="matMenu" [overlapTrigger]="false"
            class="slds-dropdown slds-dropdown_left slds-dropdown_actions">
    <ul class="slds-dropdown__list" role="menu">
      <li (click)="onClickCreerPO.emit({bail:item, nature:'Rent'})">
        <a href="javascript:void(0);">PO Loyer</a>
      </li>
      <li (click)="onClickCreerPO.emit({bail:item, nature:'AddedRent'})">
        <a href="javascript:void(0);">PO Surplus</a>
      </li>
    </ul>
  </mat-menu>
</div>

Before

It was working before update.

Current

image

elvisbegovic avatar Apr 12 '18 13:04 elvisbegovic

Same error. It seems like one Material component now has a dependency to CDK OverlayModule. Would be a breaking change, but it's not documented in the changelog. Using RC12.

cyrilletuzi avatar Apr 20 '18 08:04 cyrilletuzi

@cyrilletuzi the menu module has always had a dependency on the OverlayModule, I don't think anything has changed recently.

@istiti are you able to reproduce the error in a Stackblitz?

crisbeto avatar May 01 '18 18:05 crisbeto

For me it's not the menu, I don't use this one. But I just upgraded from Material 5 to 6, without any change in my app, and no I have the same error, needing OverlayModule. So at least one of my Material module has this new dependency.

cyrilletuzi avatar May 01 '18 19:05 cyrilletuzi

For me it's MatAutocomplete which now need OverlayModule (the error mentionsMatAutocompleteTrigger). Wasn't needed in v5 and not mentioned in documentation, no change in app.

cyrilletuzi avatar May 01 '18 19:05 cyrilletuzi

The autocomplete module has also had the OverlayModule import for a long time now. We haven't even touched that line in more than six months.

crisbeto avatar May 01 '18 19:05 crisbeto

Then there really is an issue: I have to manually add the OverlayModule in my AppModule for the mat-autocomplete to continue to work in v6, and that doesn't seem normal if it's already imported.

cyrilletuzi avatar May 01 '18 19:05 cyrilletuzi

After some tests, it appears it's only happen in v6 when the module where MatAutocomplete is used is lazy-loaded.

cyrilletuzi avatar May 01 '18 20:05 cyrilletuzi

Breaking change to my code or i used it wrongly but was working well in v5 simply adding MatMenuModule in certain lazymodule yes 😞

elvisbegovic avatar May 01 '18 21:05 elvisbegovic

This sounds like a tooling issue. @cyrilletuzi can you double-check that both Material and all other @angular/* packages are on the same major version?

crisbeto avatar May 02 '18 15:05 crisbeto

@crisbeto Yep, everything (Angular / Material / CLI) is in v6 very last RC, and issue reproduced from scratch.

cyrilletuzi avatar May 02 '18 15:05 cyrilletuzi

I've the same issue with the MatMenuTrigger. Is there any workaround, which can make it work until the problem is solved?

ghost avatar May 03 '18 08:05 ghost

I also fixed it by importing OverlayModule in my app.module.ts. Importing OverlayModule in lazy-loaded modules by the Router would't work.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";

import { OverlayModule } from "@angular/cdk/overlay";

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    OverlayModule,
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [
    AppComponent,
  ],
})
export class AppModule { }

bardiel avatar May 03 '18 09:05 bardiel

Btw, this happened to me in a brand new project generated yesterday with all @angular/*@next deps.

Angular CLI: 6.0.0-rc.9
Node: 8.11.1
OS: linux x64
Angular: 6.0.0-rc.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.5.12
@angular-devkit/build-angular     0.5.12
@angular-devkit/build-optimizer   0.5.12
@angular-devkit/core              0.5.12
@angular-devkit/schematics        0.5.12
@angular/cdk                      6.0.0-rc.14
@angular/cli                      6.0.0-rc.9
@angular/flex-layout              6.0.0-beta.15
@angular/material                 6.0.0-rc.14
@ngtools/webpack                  6.0.0-rc.10
@schematics/angular               0.5.12
@schematics/update                0.5.12
rxjs                              6.0.0
typescript                        2.7.2
webpack                           4.6.0

bardiel avatar May 03 '18 09:05 bardiel

when i import the overlay module in a lazy loading module it also dosen't work. and jsut import the OVERLAY_PROVIDERS is ok. like this

import { OverlayModule } from '@angular/cdk/overlay';

@NgModule({
  imports: [OverlayModule]
})
export class NoLazyLoadingModule { }

yokots avatar May 04 '18 09:05 yokots

Today I updated to angular 6 have the same issue,

ERROR Error: StaticInjectorError(WzzModule)[MatMenuTrigger -> Overlay]: StaticInjectorError(Platform: core)[MatMenuTrigger -> Overlay]: NullInjectorError: No provider for Overlay! at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:979) at resolveToken (core.js:1232) at tryResolveToken (core.js:1182) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077) at resolveToken (core.js:1232) at tryResolveToken (core.js:1182) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077) at resolveNgModuleDep (core.js:9238) at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:9919) at inject (core.js:1334)

yushihui avatar May 04 '18 19:05 yushihui

@yokots I tried your fix, but it does not work for me

yushihui avatar May 04 '18 20:05 yushihui

when I add import {OverlayModule} from '@angular/cdk/overlay'; to my root module it works

yushihui avatar May 04 '18 21:05 yushihui

image i guess this cause Overlay must provide in root injector.

keatkeat87 avatar May 05 '18 03:05 keatkeat87

@yushihui The OVERLAY_PROVIDERS was deprecated. Just use OverlayModule.

yokots avatar May 05 '18 08:05 yokots

What's happening is quite strange: modules using Overlay (like MatMenu or MatAutocomplete) are already importing OverlayModule. So, lazy-loading or not, it should work.

The fact that when lazy-loading a module with MatMenu or MatAutocomplete, we have this error, means that something outside the module is using Overlay, which doesn't seem normal.

cyrilletuzi avatar May 05 '18 12:05 cyrilletuzi