My Angular service is simple yet causing errors. Here's the code snippet:
// service.ts
export class SimpleService {
// ...
}
// component.ts
@Component({
selector: 'my-component',
templateUrl: 'components/mycomp/mycomp.html',
providers: [
SimpleService
]
})
class MyComponent {
constructor(private ss: SimpleService) {}
}
The above code consistently throws an error:
Uncaught Error: Can't resolve all parameters for MyComponent: (?).
Surprisingly, tweaking the constructor definition seems to fix it:
class MyComponent {
constructor(@Inject(SimpleService) private ss: SimpleService) {}
}
This contrasts with the official documentation which does not mention using @Inject
. The confusion lies in situations where primitive values or opaque tokens are injected instead of classes.
Another aspect causing uncertainty is how TypeScript annotations at runtime translate during DI processes. The Official documentation suggests that Injector uses type annotation if @Inject()
is missing. How does Angular interpret these annotations when TypeScript transpilation erases type information? Any insights?