text: Q:
  
  Reusing existing Angular Component inside material Dialog
  
  I have an existing component ResultGridComponent that is passed data by @Input(). I want to use the same component for the MatDialog. 
  Hence I added @Inject(MAT_DIALOG_DATA) data in the constructor of the ResultGridComponent component. 
  constructor(private spinner: EdpLoaderService, private dialog: MatDialog, @Optional() @Inject(MAT_DIALOG_DATA) data) {}
  
  But when the component is called as per it's regular usage that is not as a dialog, I get the following error :
  
  ERROR Error: StaticInjectorError(AppModule)[ResultGridComponent -> InjectionToken MatDialogData]: 
      StaticInjectorError(Platform: core)[ResultGridComponent -> InjectionToken MatDialogData]: 
        NullInjectorError: No provider for InjectionToken MatDialogData!
  
  I am pretty sure I have imported all the needed modules. Because I created a separate component for the dialog and passed data to it and it worked fine. But I want to reuse the component. And I am pretty sure this error is coming when I call constructor the of the child component as a regular component and the constructor doesn't get the MatDialogData passed to it by the caller.
  
  A:
  
  In some cases, I would use @Optional() before the @Inject(MAT_DIALOG_DATA). 
    constructor(
      @Optional() @Inject(MAT_DIALOG_DATA) dialogData: any
    ) {
      if (dialogData) {
       // here pass dialog data to the child component
      }
    }
  
  