ineedtoknow
ineedtoknow

Reputation: 1654

How to Use Enums for Service Method Params (in AngularProject)

I'm working in an Angular 9 project.

I have a service that I'm using in a component. A param for a method in the service is specifying the panelClass name. Instead of using strings, I'd like to use enums for this. But I'm not sure how to use enums in this way.

Here's my service, I already have my enum declared:

export enum SnackBarPanelClass {
  fail = "snack-bar-fail",
  success = "snack-bar-success"
}
@Injectable({
  providedIn: "root"
})
export class SnackBarService {

  constructor(private snackBar: MatSnackBar) {}

  showMessage(msg: string, panelClass: SnackBarPanelClass): void {
    const config = new MatSnackBarConfig();
    config.panelClass = [panelClass];
    this.snackBar.open(msg, "x", config);
  }
}

How would I get this enum from the component? Here's an example of how the component is currently using the service (with a string param instead of enum):

this.snackBarService.showMessage("There was an error getting info", "snack-bar-fail");

How would I use the enum here instead of the string?

Upvotes: 0

Views: 159

Answers (1)

Brian Rodham
Brian Rodham

Reputation: 46

this.snackBarService.showMessage("There was an error getting info", SnackBarPanelClass.fail);

Upvotes: 1

Related Questions