title: Open angular material Snackbar in service
body: <p>I want to show the user Errors through a Snackbar. Which is totally fine when I open a Snackbar through a component:</p>
  
  <pre><code>export class AppComponent implements OnInit {
  
      constructor(private snackBar: MatSnackBar) {
      }
  
      ngOnInit() {
          this.snackBar.open('message');
      }
  }
  </code></pre>
  
  <p>But when I want to open a Snackbar in my DataService it throws this error:</p>
  
  <pre><code>ERROR TypeError: Cannot read property 'open' of undefined
  </code></pre>
  
  <p>My DataService looks like this:</p>
  
  <pre><code>export class DataService {
  
    private api = 'https://localhost:5001/api/';
  
    httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };
  
    constructor(private http: HttpClient,
                private snackBar: MatSnackBar) {
    }
  
    getData() {
      return this.http.get(this.api, this.httpOptions)
        .pipe(
          retry(3), // retry a failed request up to 3 times
          catchError(this.handleError) // then handle the error
        );
    }
  
    private handleError(error: HttpErrorResponse) {
      if (error.error instanceof ErrorEvent) {
        // A client-side or network error occurred.
        console.error('An error occurred:', error.error.message);
      } else {
        // The backend returned an unsuccessful response code.
        // The response body may contain clues as to what went wrong,
        console.error(
          `Backend returned code ${error.status}, ` +
          `body was: ${error.error}`);
      }
  
      this.snackBar.open('Something bad happened, please try again later.');
  
      // return an observable with a user-facing error message
      return throwError(
        'Something bad happened; please try again later.');
    }
  }
  </code></pre>
  
  <p>So, after the third attempt of getting Data it goes into the handleError() and should open a SnackBar with "Something bad happened, please try again later."</p>