Menu

📰
0

Angular injection context lint rules: say goodbye to NG0203 error

DEV Community: angular·Cyrille Tuzi·about 1 month ago
#WoZJz9Eg
#dev#class#code#inject#injection#article
Reading 0:00
15s threshold

Angular is changing a lot these days. One of the major changes that most developers are already aware of is how to inject dependencies. // Before @ Component () export class ProductPage { constructor ( private readonly productsApi : ProductsApi ) {} } // Now @ Component () export class ProductPage { private readonly productsApi = inject ( ProductsApi ); } From compilation to runtime error The constructor-based injection was more limited, but it was safe on the compilation level. On the other hand, inject() and similar functions must be called from an injection context . Using them outside an injection context will compile just fine, but it will throw a runtime error that most developers have already encountered: the NG0203 error . This is a setback, as it is more difficult to debug, and as errors can be deployed and can happen in production. To inject or not to inject?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More