'NullInjectorError: No provider for x

Before this component was being loaded into MatDialog but now the requirement is to load it on some url i.e. http://localhost:4200/some-url and when I load this component via url and getting error

NullInjectorError: No provider for x!

Here is the component that I am trying to load on url.

export class MyComponent {
  
  constructor(
    public dialogRef: MatDialogRef<MyComponent>,
    private fb: FormBuilder,
    @Optional() @Inject(MAT_DIALOG_DATA) public data: any
  ) { }

}

enter image description here



Solution 1:[1]

I had component that had MatDialog related objects that were responsible for receiving data i.e. @Optional() @Inject(MAT_DIALOG_DATA) public data: any

Later on, with the requirement change, I had to load this component on some url instead of loading on a dialog. Just remove dialog related data and you're done.

export class MyComponent {
  
  constructor(private fb: FormBuilder) { }

}

Solution 2:[2]

Include MatDialogModule in imports array in the module in which this component is registered.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WasiF
Solution 2 Suraj Kumar