Angular 2 - Named router outlets without horrible URLs

后端 未结 3 1802
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 00:52

I want to have two router outlets, the primary one, and a modal outlet. When navigating to /login, I want to show my home component in the primary outlet, and m

相关标签:
3条回答
  • 2021-01-01 01:38

    You can pass NavigationExtras with skipLocationChange: true to hide the ugly url for your users.

    const navigationExtras: NavigationExtras = {
      skipLocationChange: true,
    };
    this.router.navigate([{ outlets: { modal: 'popup' } }], navigationExtras);
    

    You can also rename the name of your router outlet from model to something more user friendly like popup:

    /home(popup:login)
    

    But then again, maybe that is not pretty enough :D

    0 讨论(0)
  • 2021-01-01 01:42

    FYI, we encountered the same problem and solved it according to this response: https://stackoverflow.com/a/58915611/9260456

    0 讨论(0)
  • 2021-01-01 01:46

    Why don't you go for component less routes,

    {
        path: 'login',
        children: [
            {
                path: '',
                component: HomeComponent
            },
            {
                path: '',
                component: LoginComponent,
                outlet: 'modal'
            }
        ]
    }
    

    This will make sure that when your route is /login, HomeComponent will go in primary outlet and LoginComponent will go to router-outlet with name 'modal'.

    0 讨论(0)
提交回复
热议问题