Coder Social home page Coder Social logo

use createComponent and ApplicationRef.attachView, the primary router outlet will to be replaced if the dynamically created component contains router-outlet about angular HOT 9 CLOSED

why520crazy avatar why520crazy commented on April 28, 2024
use createComponent and ApplicationRef.attachView, the primary router outlet will to be replaced if the dynamically created component contains router-outlet

from angular.

Comments (9)

JeanMeche avatar JeanMeche commented on April 28, 2024

Hi, is there a reason you're using createComponent + applicationRef.attachView instead of viewContainerRef.createComponent. Because there is no issue in the latter.

from angular.

why520crazy avatar why520crazy commented on April 28, 2024

Hi, is there a reason you're using createComponent + applicationRef.attachView instead of viewContainerRef.createComponent. Because there is no issue in the latter.

Yes

from angular.

JeanMeche avatar JeanMeche commented on April 28, 2024

What would be the particular usecase your are looking to address with this?

from angular.

atscott avatar atscott commented on April 28, 2024

Closing as expected. This is effectively the same thing as having two router outlets.

from angular.

alxhub avatar alxhub commented on April 28, 2024

Note that as @atscott mentions, this is effectively replacing the primary router outlet, as it's created as a root-level outlet.

To create it as a child outlet of the root outlet instead, you would need to create the new component as a DI child of the parent RouterOutlet. This is done by passing elementInjector as well as environmentInjector when calling createComponent. See: https://stackblitz.com/edit/ng-create-component-with-outer-outlet-gzentc?file=src%2Fdashboard.ts

from angular.

why520crazy avatar why520crazy commented on April 28, 2024

@alxhub @atscott @JeanMeche thank you very much for providing the solution.

  1. Firstly, the current expectation is for version 16.0 to appear, in 15.x it don't replace the primary router outlet, it looks like a breaking change.
  2. We are using createComponent + applicationRef.attachView when using a micro front-end for cross application rendering, App1 renders component of another application (App2), App1 can only obtain the applicationRef of App2, If elementInjector can be passed, then simply use viewContainerRef.createComponent instead of the global createComponent.
  3. If this is a expected behavior, are there other alternative solutions besides passing elementInjector.

from angular.

atscott avatar atscott commented on April 28, 2024

Firstly, the current expectation is for version 16.0 to appear, in 15.x it don't replace the primary router outlet, it looks like a breaking change.

Your example seems to have the same behavior in 15: https://stackblitz.com/edit/angular-3eqbcp

App1 renders component of another application (App2), App1 can only obtain the applicationRef of App2

This seems a bit sketchy. Why is this the case and why aren't the ApplicationRef instances the same? Regardless, I'm not really following why App1 cannot create the App2 component and always pass the elementRef so that the ElementRef hierarchy is preserved for RouterOutlet. This is required to avoid same-name collisions of outlets.

If this is a expected behavior, are there other alternative solutions besides passing elementInjector.

There should be other workarounds. For example, preventing App2 from rendering another outlet with the same name.

from angular.

why520crazy avatar why520crazy commented on April 28, 2024

@atscott

Your example seems to have the same behavior in 15: https://stackblitz.com/edit/angular-3eqbcp

Sorry for my mistake, i only test it use NgModule in 15 : https://stackblitz.com/edit/angular-3tn8ws, I didn't expect that standalone component application would be inconsistent with NgModule app.

This seems a bit sketchy. Why is this the case and why aren't the ApplicationRef instances the same? Regardless, I'm not really following why App1 cannot create the App2 component and always pass the elementRef so that the ElementRef hierarchy is preserved for RouterOutlet. This is required to avoid same-name collisions of outlets.

App1 and App2 are different angular application, they all have independent AppModule and router. I found this issue is due to using the example of @worktile/planet, I open http://planet.ngnice.com/app1/projects after the page, when navigate to App2 again, a blank page appears:
image

because the /app1/projects page renders a component of App2, and the component template of App2 includes a router-outlet.

There should be other workarounds. For example, preventing App2 from rendering another outlet with the same name.

I think this is a good idea

from angular.

angular-automatic-lock-bot avatar angular-automatic-lock-bot commented on April 28, 2024

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.