I have this component using a value provider:
import { Component, OnInit, Injector } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [{
provide: "currency",
useValue : "dollar"
}]
})
export class AppComponent implements OnInit {
ngOnInit(): void {
}
title = "app works";
constructor(private injector: Injector){
this.title = "Currency is: "+ injector.get("currency");
}
}
When I run the program, the injector throws an error saying, "Cannot read property 'instance' of undefined"
If it matters, my view is a simple <h1>{{title}}</h1>.
What I'm I doing wrong?
Don't use Injector, just import the 'Inject' decorator and use like so-
import { Component, OnInit, Inject } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [{
provide: "currency",
useValue : "dollar"
}]
})
export class AppComponent implements OnInit {
ngOnInit(): void {
}
title = "app works";
constructor(@Inject("currency") private currency){
this.title = "Currency is: "+ currency;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With