vaadinvaadin20vaadin-fusionhilla

Vaadin: Automatically route to a child component


I am following a basic routing setup as described in the docs where you have an umbrella component and then multiple child components that render into a <slot> of the umbrella component.

So the HTML Template of the main x-layout component looks somewhat like this:

<vaadin-app-layout>
  <slot></slot>
</vaadin-app-layout>

and my routes look somewhat like this:

router.setRoutes([
    {path: '/',
      component: 'x-layout',
      children: [
        {path: '/users', component: 'x-user-list'},
        {path: '/rooms', component: 'x-rooms-list'},
      ]
    }
  ]);

In contrast to the examples in the docs I want to immediately load one of the child components (say, x-user-list) when the application is invoked with its root URL ('/'), and without the user selecting a link from a navigation bar or so. So I need a means to automatically invoke a route from the main component. How can I achieve that?


Solution

  • Try the following structure for the routes:

    export const views: ViewRoute[] = [
      {
        path: '',
        component: 'x-user-list',
        title: '',
      },
      {
        path: 'userlist',
        component: 'x-user-list',
        title: 'User List',
      },
    ];
    export const routes: ViewRoute[] = [
      {
        path: '',
        component: 'x-layout',
        children: [...views],
      },
    ];