@@ -65,6 +65,26 @@ Demonstre diferenças criando múltiplos componentes e módulos lazy-loaded.
6565** root.service.ts**
6666import { Injectable } from '@angular/core ';
6767
68+ @Injectable ({
69+ providedIn: 'root'
70+ })
71+ export class RootService {
72+ private instanceId = Math.random().toString(36).substr(2, 9);
73+
74+ constructor() {
75+ console.log(` [ROOT] RootService criado: ${this.instanceId} ` );
76+ }
77+
78+ getInstanceId(): string {
79+ return this.instanceId;
80+ }
81+
82+ getScope(): string {
83+ return 'root - Singleton global';
84+ }
85+ }
86+ import { Injectable } from '@angular/core ';
87+
6888@Injectable ({
6989 providedIn: 'root'
7090})
@@ -109,6 +129,26 @@ export class RootService {
109129** any.service.ts**
110130import { Injectable } from '@angular/core ';
111131
132+ @Injectable ({
133+ providedIn: 'any'
134+ })
135+ export class AnyService {
136+ private instanceId = Math.random().toString(36).substr(2, 9);
137+
138+ constructor() {
139+ console.log(` [ANY] AnyService criado: ${this.instanceId} ` );
140+ }
141+
142+ getInstanceId(): string {
143+ return this.instanceId;
144+ }
145+
146+ getScope(): string {
147+ return 'any - Nova instância por módulo lazy';
148+ }
149+ }
150+ import { Injectable } from '@angular/core ';
151+
112152@Injectable ({
113153 providedIn: 'any'
114154})
@@ -153,6 +193,24 @@ export class AnyService {
153193** component-scoped.service.ts**
154194import { Injectable } from '@angular/core ';
155195
196+ @Injectable ()
197+ export class ComponentScopedService {
198+ private instanceId = Math.random().toString(36).substr(2, 9);
199+
200+ constructor() {
201+ console.log(` [COMPONENT] ComponentScopedService criado: ${this.instanceId} ` );
202+ }
203+
204+ getInstanceId(): string {
205+ return this.instanceId;
206+ }
207+
208+ getScope(): string {
209+ return 'component - Nova instância por componente';
210+ }
211+ }
212+ import { Injectable } from '@angular/core ';
213+
156214@Injectable ()
157215export class ComponentScopedService {
158216 private instanceId = Math.random().toString(36).substr(2, 9);
@@ -206,20 +264,35 @@ import { ComponentScopedService } from './component-scoped.service';
206264
207265 <div class =" service-info " >
208266 <h3 >RootService (providedIn: 'root')</h3 >
267+ {% raw %}
268+
209269 <p>ID: {{ rootService.getInstanceId() }}</p>
270+ {% endraw %}
271+
210272 <p>{{ rootService.getScope() }}</p>
273+
211274 </div>
212275
213276 <div class="service-info">
214277 <h3>AnyService (providedIn: 'any')</h3>
278+ {% raw %}
279+
215280 <p>ID: {{ anyService.getInstanceId() }}</p>
281+ {% endraw %}
282+
216283 <p>{{ anyService.getScope() }}</p>
284+
217285 </div>
218286
219287 <div class="service-info">
220288 <h3>ComponentScopedService (component providers)</h3>
289+ {% raw %}
290+
221291 <p>ID: {{ componentService.getInstanceId() }}</p>
292+ {% endraw %}
293+
222294 <p>{{ componentService.getScope() }}</p>
295+
223296 </div>
224297
225298 <app-demo-child></app-demo-child>
@@ -234,6 +307,48 @@ export class DemoComponent {
234307 ) {}
235308}
236309{% raw %}
310+ import { Component } from '@angular/core ';
311+ import { RootService } from './root.service';
312+ import { AnyService } from './any.service';
313+ import { ComponentScopedService } from './component-scoped.service';
314+
315+ @Component ({
316+ selector: 'app-demo',
317+ standalone: true,
318+ providers: [ ComponentScopedService] ,
319+ template: `
320+ <div class =" demo " >
321+ <h2 >Demonstração de Escopos</h2 >
322+
323+ <div class =" service-info " >
324+ <h3 >RootService (providedIn: 'root')</h3 >
325+ <p >ID: {{ rootService.getInstanceId() }}</p >
326+ <p >{{ rootService.getScope() }}</p >
327+ </div >
328+
329+ <div class =" service-info " >
330+ <h3 >AnyService (providedIn: 'any')</h3 >
331+ <p >ID: {{ anyService.getInstanceId() }}</p >
332+ <p >{{ anyService.getScope() }}</p >
333+ </div >
334+
335+ <div class =" service-info " >
336+ <h3 >ComponentScopedService (component providers)</h3 >
337+ <p >ID: {{ componentService.getInstanceId() }}</p >
338+ <p >{{ componentService.getScope() }}</p >
339+ </div >
340+
341+ <app-demo-child ></app-demo-child >
342+ </div >
343+ `
344+ })
345+ export class DemoComponent {
346+ constructor(
347+ public rootService: RootService,
348+ public anyService: AnyService,
349+ public componentService: ComponentScopedService
350+ ) {}
351+ }
237352``` typescript
238353import { Component } from ' @angular/core' ;
239354import { RootService } from ' ./root.service' ;
@@ -293,9 +408,15 @@ import { ComponentScopedService } from './component-scoped.service';
293408 template: `
294409 <div class =" child-demo " >
295410 <h3 >Componente Filho</h3 >
411+ {% raw %}
412+
296413 <p>RootService ID: {{ rootService.getInstanceId() }} (mesmo)</p>
414+ {% endraw %}
415+
297416 <p>AnyService ID: {{ anyService.getInstanceId() }} (mesmo se mesmo módulo)</p>
417+
298418 <p>ComponentScopedService ID: {{ componentService.getInstanceId() }} (novo)</p>
419+
299420 </div>
300421 `
301422})
@@ -307,6 +428,31 @@ export class DemoChildComponent {
307428 ) {}
308429}
309430{% raw %}
431+ import { Component } from '@angular/core ';
432+ import { RootService } from './root.service';
433+ import { AnyService } from './any.service';
434+ import { ComponentScopedService } from './component-scoped.service';
435+
436+ @Component ({
437+ selector: 'app-demo-child',
438+ standalone: true,
439+ providers: [ ComponentScopedService] ,
440+ template: `
441+ <div class =" child-demo " >
442+ <h3 >Componente Filho</h3 >
443+ <p >RootService ID: {{ rootService.getInstanceId() }} (mesmo)</p >
444+ <p >AnyService ID: {{ anyService.getInstanceId() }} (mesmo se mesmo módulo)</p >
445+ <p >ComponentScopedService ID: {{ componentService.getInstanceId() }} (novo)</p >
446+ </div >
447+ `
448+ })
449+ export class DemoChildComponent {
450+ constructor(
451+ public rootService: RootService,
452+ public anyService: AnyService,
453+ public componentService: ComponentScopedService
454+ ) {}
455+ }
310456``` typescript
311457import { Component } from ' @angular/core' ;
312458import { RootService } from ' ./root.service' ;
0 commit comments