Angular 6 service with interface

前端 未结 4 1286
旧时难觅i
旧时难觅i 2021-01-02 04:00

I am building an application with Angular (6.0.7) and I am trying to create a service with the new:

@Injectable({
  providedIn: \'root\'
})
         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 04:25

    This can be done with InjectionToken, which is a replacement for the obsolete OpaqueToken

    export const AuthenticationProvider = new InjectionToken(
      "AuthenticationProvider",
      { providedIn: "root", factory: () => new CognitoAuthenticationProvider() }
    );
    
    ...
    
    @Injectable()
    export class CognitoAuthenticationProvider implements IAuthenticationProvider {
    
    ...
    
    @Injectable({
      providedIn: "root"
    })
    export class AuthenticationService {
      constructor(
        @Inject(AuthenticationProvider)
        private authenticationProvider: IAuthenticationProvider,
        private http: HttpClient
      ) {}
    

提交回复
热议问题