May you explain me some fact? I have this code
sharedservice.ts
<code>
@Injectable({
providedIn: 'root'
})
export class SharedService {
id = (new Date()).getTime();
}
</code>
<code>
@Injectable({
providedIn: 'root'
})
export class SharedService {
id = (new Date()).getTime();
}
</code>
@Injectable({
providedIn: 'root'
})
export class SharedService {
id = (new Date()).getTime();
}
main.ts
<code>export const ROUTES: Route[] = [
{
path: 'lazycomp',
loadComponent: () =>
import('./lazycomp.component').then(
(mod) => mod.LazycompComponent
),
},
];
@Component({
selector: 'app-root',
standalone: true,
template: `
{{serv.id}}
@if(!(loaded$ | async)) {
<button (click)="load()">
Load advanced settings
</button>
}
<ng-container *ngComponentOutlet="comp" />
`,
imports:[CommonModule],
providers: []
})
export class App {
loaded$ = new BehaviorSubject<boolean>(false);
comp: any;
readonly serv = inject(SharedService);
async load() {
this.comp = await import('./lazycomp.component').then((mod) => mod.LazycompComponent);
console.log(this.comp);
this.loaded$.next(true);
}
}
bootstrapApplication(App);
</code>
<code>export const ROUTES: Route[] = [
{
path: 'lazycomp',
loadComponent: () =>
import('./lazycomp.component').then(
(mod) => mod.LazycompComponent
),
},
];
@Component({
selector: 'app-root',
standalone: true,
template: `
{{serv.id}}
@if(!(loaded$ | async)) {
<button (click)="load()">
Load advanced settings
</button>
}
<ng-container *ngComponentOutlet="comp" />
`,
imports:[CommonModule],
providers: []
})
export class App {
loaded$ = new BehaviorSubject<boolean>(false);
comp: any;
readonly serv = inject(SharedService);
async load() {
this.comp = await import('./lazycomp.component').then((mod) => mod.LazycompComponent);
console.log(this.comp);
this.loaded$.next(true);
}
}
bootstrapApplication(App);
</code>
export const ROUTES: Route[] = [
{
path: 'lazycomp',
loadComponent: () =>
import('./lazycomp.component').then(
(mod) => mod.LazycompComponent
),
},
];
@Component({
selector: 'app-root',
standalone: true,
template: `
{{serv.id}}
@if(!(loaded$ | async)) {
<button (click)="load()">
Load advanced settings
</button>
}
<ng-container *ngComponentOutlet="comp" />
`,
imports:[CommonModule],
providers: []
})
export class App {
loaded$ = new BehaviorSubject<boolean>(false);
comp: any;
readonly serv = inject(SharedService);
async load() {
this.comp = await import('./lazycomp.component').then((mod) => mod.LazycompComponent);
console.log(this.comp);
this.loaded$.next(true);
}
}
bootstrapApplication(App);
lazycomp.component
<code>@Component({
selector: 'app-lazycomp',
standalone: true,
imports: [LazycompimpComponent],
template: `
lazy = {{serv.id}}
`,
styles: ``,
providers: []
})
export class LazycompComponent {
readonly serv = inject(SharedService);
}
</code>
<code>@Component({
selector: 'app-lazycomp',
standalone: true,
imports: [LazycompimpComponent],
template: `
lazy = {{serv.id}}
`,
styles: ``,
providers: []
})
export class LazycompComponent {
readonly serv = inject(SharedService);
}
</code>
@Component({
selector: 'app-lazycomp',
standalone: true,
imports: [LazycompimpComponent],
template: `
lazy = {{serv.id}}
`,
styles: ``,
providers: []
})
export class LazycompComponent {
readonly serv = inject(SharedService);
}
lazycompimp.component
<code>@Component({
selector: 'app-lazycompimp',
standalone: true,
imports: [],
template: ``,
styles: ``,
providers: [SharedService]
})
export class LazycompimpComponent {
}
</code>
<code>@Component({
selector: 'app-lazycompimp',
standalone: true,
imports: [],
template: ``,
styles: ``,
providers: [SharedService]
})
export class LazycompimpComponent {
}
</code>
@Component({
selector: 'app-lazycompimp',
standalone: true,
imports: [],
template: ``,
styles: ``,
providers: [SharedService]
})
export class LazycompimpComponent {
}
I press button, load lazycomp
and get 1717188440363 lazy = 1717188440363
.
Ok. service instance is singleton and on root injector.
After that I add providers: [SharedService]
to lazycomp
and get 1717189116834 lazy = 1717189194305
How it work? Why imports: [LazycompimpComponent]
in lazycomp does not put SharedService to providers?
I thought that lazy loaded module (standalone component module too?) create own root injector.