Created
September 13, 2018 13:57
-
-
Save tomtobac/77946e7592d33fdcf13f0e6fbe40222a to your computer and use it in GitHub Desktop.
Singelton typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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