Avatar ·

Angular में इंजेक्शन के दौरान ngOnInit क्यों नहीं कॉल होता?

नमस्ते! मैं Angular और TypeScript में नया हूँ, और एक अजीब समस्या का सामना किया। मेरे पास एक service है जिसे बनाते समय कुछ initialization करनी चाहिए। मैंने Angular hooks, विशेष रूप से `ngOnInit` का उपयोग करने का निर्णय लिया ताकि यह कोड स्वचालित रूप से चले। लेकिन यह काम नहीं करता — `ngOnInit` method बस कॉल नहीं होता। यहाँ मेरा कोड है:\n\n```typescript\nimport { Injectable, OnInit } from '@angular/core';\n\n@Injectable({\n providedIn: 'root'\n})\nexport class DataService implements OnInit {\n public data: any[] = [];\n\n constructor() {\n console.log('Constructor called');\n }\n\n ngOnInit(): void {\n console.log('ngOnInit called');\n this.data = ['item1', 'item2'];\n }\n}\n```\n\nमैं उम्मीद करता हूँ कि जब service को component में inject किया जाएगा, तो `ngOnInit` स्वचालित रूप से कॉल हो जाएगा, लेकिन कोड में