Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: asyncInject (or deferInject) #62066

Open
@cesco69

Description

@cesco69

Hi,
I have some services only used at some condition (eg.: button click). Those services affect bundle size. The idea is introduce asyncInject (or deferInject) for injecting (and download the javascript chunk) only when necessary (similar concept of @defer https://angular.dev/guide/templates/defer)

Proposed solution

import { Component, asyncInject /** or deferInject */  } from '@angular/core';
import { BarService } from './bar.service';

@Component({
    selector: 'foo',
    template: `<button (click)="onClick()">click</button>`,
    providers: [
        BarService,
    ],
})
export class FooComponent  {
    barService: Promise<BarService> = asyncInject(BarService);

    onClick(){
        (await this.barService).hello();
    }
}

This is just a POC https://stackblitz.com/edit/stackblitz-starters-rhaxw1jj

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimefeatureIssue that requests a new feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions