Instantiate class on application start

Asked

Viewed 559 times

0

I would like to know if it is possible to instantiate a class and use it throughout my application without having to instantiate it and import it. I have my app.:

import { Component, OnInit } from '@angular/core';
import { AnalyticsService } from './@core/utils/analytics.service';

@Component({
  selector: 'app',
  template: '<router-outlet></router-outlet>',
})
export class AppComponent implements OnInit {

  constructor(private analytics: AnalyticsService) {
  }

  ngOnInit(): void {
    this.analytics.trackPageViews();
  }
}

I have my app.module:

import { APP_BASE_HREF } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { CoreModule } from './@core/core.module';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { ThemeModule } from './@theme/theme.module';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { LoginComponent } from './pages/autenticacao/login.component';
import { AuthService } from './app.setings';

@NgModule({
  declarations: [ AppComponent, LoginComponent ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpModule,
    AppRoutingModule,

    NgbModule.forRoot(),
    ThemeModule.forRoot(),
    CoreModule.forRoot(),
  ],
  bootstrap: [AppComponent],
  providers: [
    { provide: APP_BASE_HREF, useValue: '/' },
  ],
})
export class AppModule {
}

And finally I have my Authservice

export class AuthService {
    // logica do classe aqui 
}

Basically wanted to create an instance of AuthService (new AuthService()) in app.Component or app.module and therefore whenever I need to use the Authservice class use the one that was created when starting my application, without needing to import it and create a new instance of it.

Code like this:

    import { API_CONFIG, AuthService } from '../../../app.setings';

@Injectable()
export class CidadeService {

  constructor(private authService: AuthService) {

    // criei uma nova intancia de AuthService
  }

}

I want to be able to import it in app.module or app.Component and whenever I need to simply call and use it

@Injectable()
export class CidadeService {

    metodo() {
        AuthService.outroMetodo();
    }

}
  • 1

    In fact you are not creating a new instance, Angular keeps the same instance in memory and always gives you that same instance when you call from the Component constructor.

  • I got the expected result kind of by a gambiara, see if there’s any better way to do it. I pulled the export of the class and at the end of the file put const _AuthService = new AuthService();&#xA;export const authService = _AuthService;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.