最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NullInjectorError: No provider for MatBottomSheetRef - Stack Overflow

programmeradmin1浏览0评论

In this stackblitz I'm getting the following error (Even though the MatBottomSheetModule is imported):

        ERROR
        Error: StaticInjectorError(AppModule)[CountryCodeSelectComponent -> MatBottomSheetRef]:
        StaticInjectorError(Platform: core)[CountryCodeSelectComponent -> MatBottomSheetRef]:
        NullInjectorError: No provider for MatBottomSheetRef!

Thoughts?

Angular / Components Feature Request

If you agrre you can vote for this. I requested making the use case symmetrical WRT how we make use of other similar Angular components / services.

In this stackblitz I'm getting the following error (Even though the MatBottomSheetModule is imported):

        ERROR
        Error: StaticInjectorError(AppModule)[CountryCodeSelectComponent -> MatBottomSheetRef]:
        StaticInjectorError(Platform: core)[CountryCodeSelectComponent -> MatBottomSheetRef]:
        NullInjectorError: No provider for MatBottomSheetRef!

Thoughts?

Angular / Components Feature Request

If you agrre you can vote for this. I requested making the use case symmetrical WRT how we make use of other similar Angular components / services.

https://github.com/angular/components/issues/17011

Share Improve this question edited Feb 19, 2022 at 14:14 Lajos Arpad 76.6k40 gold badges115 silver badges220 bronze badges asked Sep 8, 2019 at 23:38 OleOle 47k68 gold badges237 silver badges441 bronze badges 1
  • I actually injected MatBottomSheet instead of MatBottomSheetRef and called the dismiss() method on MatBottomSheet. It worked. – Sandeep Commented Feb 16, 2022 at 5:53
Add a comment  | 

2 Answers 2

Reset to default 18

You need providers for Bottom Sheet which you were missing.

Make these changes to material.module.ts

import {MatBottomSheetRef, MAT_BOTTOM_SHEET_DATA} from '@angular/material/bottom-sheet';

@NgModule({
  exports: [...]
  providers: [
    { provide: MatBottomSheetRef, useValue: {} },
    { provide: MAT_BOTTOM_SHEET_DATA, useValue: {} }
  ],
})

This is the recommended approach for Treeshaking for webpack for more performant and minimized bundles:

In file: country-code-select.component.ts

// Removed -1
// import { MatBottomSheetRef } from '@angular/material'; 


//Added +1
import { MatBottomSheetRef} from '@angular/material/bottom-sheet';  

The Stackblitz for your reference

https://stackblitz.com/edit/angular-material-baseline2-country-code-select-6hisdk

And you are set. Cheers!

The accepted answer works but you will get the this._bottomSheet.open is not a function error, simplest solution will be to:

import {MatBottomSheetModule} from '@angular/material/bottom-sheet'; in your component module or app module, and at it to your imports

@NgModule({
  imports: [
    ...,
    MatBottomSheetModule,
    ...,
  ],
  declarations: [...],
})
发布评论

评论列表(0)

  1. 暂无评论