Skip to content

Instantly share code, notes, and snippets.

@tomtobac
Created September 13, 2018 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomtobac/77946e7592d33fdcf13f0e6fbe40222a to your computer and use it in GitHub Desktop.
Save tomtobac/77946e7592d33fdcf13f0e6fbe40222a to your computer and use it in GitHub Desktop.
Singelton typescript
class AuthService {
private static instance: AuthService;
private _counter: number = 0;
constructor() {
console.log('AuthService constructor')
}
public static getInstance() {
console.log('AuthService getInstance')
if (!AuthService.instance) {
AuthService.instance = new AuthService();
}
return AuthService.instance;
}
set counter(num: number) {
this._counter = num;
}
get counter() {
return this._counter;
}
public increateCounter() {
this.counter = this.counter + 1;
}
public showCounter() {
return 'Counter: ' + this.counter;
}
}
export default AuthService.getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment