Main.module.ts
@NgModule({
declarations:[HomeComponent,MasterComponent,],
imports:[
BrowserModule,
FormsModule,
RouterModule.forRoot(MainRoutes)
],
providers:[
{
provide:BaseLogger,
useClass:DBLogger
},
{provide:"1",useClass:DBLogger},
{provide:"2",useClass:ConsoleLogger},
],
bootstrap:[MasterComponent]
})
export class MainModule{
}
***customer.component.ts***
I just injection but provide value not shown in the constructor of the component
@Component({
selector: 'cust-app',
templateUrl: './customer.view.html',
styleUrls: ['./customer.view.css']
})
export class CustomerComponent {
title: string = 'Customer Application';
customerModel:Customer = new Customer();
customerModels:Array<Customer> = new Array<Customer>();
loggerObj: BaseLogger = null;
constructor(_injector: Injector) {
this.loggerObj = _injector.get("1");
-- Show error get is deprecated I got an error here I just want to provide value
I do not know how
to inject
this.loggerObj.Log();
}
}
CustomerApp.logger.ts
---------------------
export interface ILogger{
Log():void;
}
export class BaseLogger implements ILogger{
Log(){
}
}
export class ConsoleLogger extends BaseLogger{
Log(){
console.log('Using Console Logger..');
}
}
export class DBLogger extends BaseLogger{
Log(){
console.log('Using Database Logger');
}
}
export class FileLogger extends BaseLogger{
Log():any{
console.log('Using File Logger');
}
}
Error :
You can see below code I am trying to use the injector in the constructor
but I did not get("1")
I am struggling to do in
get method is deprecated so how to get 1 value from providing in the constructor of component.
what I want if the value of providing which is present in providers:[] of the main module I want
this "1"
but nothing happens