Content-Length: 242537 | pFad | http://github.com/angular/angular/issues/61752

D0 Create application ref for each custom component · Issue #61752 · angular/angular · GitHub
Skip to content

Create application ref for each custom component #61752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MillerSvt opened this issue May 29, 2025 · 0 comments
Open

Create application ref for each custom component #61752

MillerSvt opened this issue May 29, 2025 · 0 comments
Labels
area: elements Issues related to Angular Elements
Milestone

Comments

@MillerSvt
Copy link

MillerSvt commented May 29, 2025

Which @angular/* package(s) are relevant/related to the feature request?

elements

Description

Right now it is not possible to create separate applications per component.

We create web component with router, and implements own LocationStrategy.

const appRef = await createApplication(
    mergeApplicationConfig(appConfig, {
        providers: [
            provideExperimentalZonelessChangeDetection(),
            provideRouter(
                [
                    {
                        path: ``,
                        component: TestComponent,
                    },
                    {
                        path: `2`,
                        component: Test2Component,
                    },
                ],
            ),
            provideEnvironmentInitializer(() => {
                inject(Router).initialNavigation();
            }),
            {
                provide: LocationStrategy,
                useClass: CustomLocationStrategy,
            },
        ],
    }),
);

const element = createCustomElement(MessengerComponent, {
    injector: appRef.injector,
});

customElements.define(`t-messenger`, element);

It works perfectly. But only if one component on page. If we attach more component, then router executes only on last custom component per page.

I believe that creating separate appRef's for each web component will solve the problem.

Proposed solution

export type NgElementConfig  = {
    /**
     * An optional custom strategy factory to use instead of the default.
     * The strategy controls how the transformation is performed.
     */
    strategyFactory?: NgElementStrategyFactory;
} & (
    {
        /**
         * The injector to use for retrieving the component's factory.
         */
        injector: Injector;
    } | {
        /**
         * The config for application.
         */
        applicationConfig: ApplicationConfig;
    }
);

And we can define custom component by:

const element = createCustomElement(MessengerComponent, {
    applicationConfig: mergeApplicationConfig(appConfig, {
        providers: [
            provideExperimentalZonelessChangeDetection(),
            provideRouter(
                [
                    {
                        path: ``,
                        component: TestComponent,
                    },
                    {
                        path: `2`,
                        component: Test2Component,
                    },
                ],
            ),
            provideEnvironmentInitializer(() => {
                inject(Router).initialNavigation();
            }),
            {
                provide: LocationStrategy,
                useClass: CustomLocationStrategy,
            },
        ],
    })
});

customElements.define(`t-messenger`, element);

Alternatives considered

Fork

MillerSvt added a commit to MillerSvt/angular that referenced this issue May 29, 2025
@MillerSvt MillerSvt changed the title Create application per custom component Create application ref for each custom component May 30, 2025
@kirjs kirjs added the area: elements Issues related to Angular Elements label May 30, 2025
@ngbot ngbot bot added this to the needsTriage milestone May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: elements Issues related to Angular Elements
Projects
None yet
Development

No branches or pull requests

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/issues/61752

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy