Instantiate class when starting application

0

I wonder if you can instantiate a class and use it throughout my application without having to instantiate it and import it. I have my app.component:

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 I 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 this one that was created when I started my application, without needing it import it and create a new instance of it.

Code looks 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 into app.module or app.component and whenever I need it I simply call and use it

@Injectable()
export class CidadeService {

    metodo() {
        AuthService.outroMetodo();
    }

}
    
asked by anonymous 08.01.2018 / 16:44

0 answers