Viewports
Learn about viewports in Router-Lite and how to configure hierarchical routing.
The <au-viewport>
element, or commonly referred to as viewport (not to confuse with viewport
meta tag), is the "outlet", where the router-lite attaches/loads the components. For a basic example of viewport, please refer the "Getting started"-tutorial. Most of the examples in the preceding sections show the usage of only a single viewport. However, you can use multiple viewports with the sibling viewports and hierarchical routing. These are useful to create different routing layouts. In the subsequent sections, we first discuss about that. Later in this part of the documentation, we focus on the different configuration options available for viewports.
Hierarchical routing
As seen in the "Getting started"-tutorial, a component can define a set of children routes (using either the @route
decorator or the static properties). The child routes can also in turn define children routes of there own. Such route configuration are commonly known as hierarchical route configuration.
To understand it better, let us consider an example. We want to show a list of products, as links, and when a product link is clicked, we display the details of the product.
To this end we want a viewport on the root component which is used to host the list component. The list component itself houses anther viewport, or more accurately a child viewport, which is then used to host the product details.
The route configuration for the root component consists of a single root for the list component and it also houses a single viewport. The Products
component defines its own child route configuration and houses a child viewport. This is shown below.
The IProductService
injected to the Products
is just some service used to populate the product list and has very little relevance to the discussion related to the router. And the extra style is added to display the list and the details side by side. And that's pretty much all you need for a simple hierarchical routing configuration. You can see this in action below.
If you open the example in a new tab, you can see how the URL paths are constructed. For example, when you click a product link, the URL is /42/details
or /products/42/details
. This also means that when you try to navigate to that URL directly, the product details will be loaded from the start. It essentially creates shareable URLs.
Sibling viewports
Two viewports are called siblings if they are under one parent routing hierarchy. Let us recreate the previous example but using sibling viewports this time.
To this end, we want to viewports, housed side-by-side on the root component. We show the products' list on one viewport and the details of the selected product on the other one.
To this end, let us start with the routing configuration on the root component.
Two routes, one for the list another for the details, are configured on the route. Next we need 2 viewports to on the root component. Let us get that done.
Even though we are not yet done, you can check out our work so far in the live example below.
If you run the example, you can immediately see a "problem" that both the viewports are loading the products' list. Although it is not an error per se, with natural use-case in mind, you probably like to avoid that. Let us fix this "problem" first.
This is happening due to the default value of the default
attribute of the <au-viewport>
that is set to ''
(empty string). This default value enables loading the component associated with the empty path without any additional configuration. This default behavior makes sense as the usage of a single viewport at every routing hierarchy might be prevalent.
However, we need a way to prevent this duplication. To this end, we can bind null
to the default
attribute of a viewport, which instructs the router-lite that this particular viewport should be left out when it is empty (that is no component is targeted to be loaded in this viewport).
You can see in the live example below that this fixes the duplication issue.
We still need a way to load the product details on the second viewport. Note that till now, the two viewports cannot be referentially differentiated from one another; that is if you want to load a component specifically on the first or on the second viewport, there is no way to do this for now. To this end, we need to name the viewports.
Although we name the viewports semantically, it is not necessary, and you are free to choose viewport names, as you like. Lastly, we need to use the load
attribute in the Products
component to construct the URL, or more accurately the routing instruction correctly, such that the details of the product is loaded on the details
viewport.
Using the load
attribute we are instructing the router-lite to load the Product
(using the route-id details
) component, with the id
parameter of the route set to the id
of the current item
in the repeater, in the details
viewport. With the context.bind:null
, we are instructing the router-lite to perform this routing instruction on the root routing context (refer the documentation for the load
attribute for more details). Now, when someone clicks a product link the associated details are loaded in the details
viewport. You can see this in action below.
If you open the example in a new tab, you can see how the URL paths are constructed. For example, when you click a product link, the URL is /details/42@details+products@list
.
Named viewports
As seen in the sibling viewports example, viewports can be named. It is particularly useful when there are multiple sibling viewports present. Note that specifying a value for the name
attribute of viewport is optional, and the default value is simply 'default'
.
In the following example, we have the main
viewport for our main content and then another viewport called sidebar
for our sidebar content.
Using viewport name for routing instructions
The names can be used to instruct the router-lite to load a specific component to a specific named viewport. To this end the path syntax is as follows:
The live example below shows this.
Note the load
attributes in the anchor
elements.
In the example, clicking the first anchor loads the products
component in the list
viewport and the details of the product with #{id
} into the details
viewport. The second anchor facilitates loading only the the details of the product with #{id
} into the details
viewport.
For more details about navigating and instructions for router-lite, please refer the documentation.
Specifying a viewport name on a route
By default, the routes/components are loaded into the first available viewport, when there is no viewport instruction is present. However, the routes can also be configured, such that a configured route is allowed to be loaded only in a certain viewport. This is useful when you know that a certain component needs to be loaded in a certain viewport, because in that case you can use the simple {path}
instruction instead of the more verbose alternative, the {path}@{viewport-name}
instruction. To this end, use the viewport
option of the route configuration.
In this example, we are specifying that the Products
component needs to be loaded into the list
viewport and the Product
component need to be loaded into the details
viewport. You can also see this in the live example below.
Note the anchor
s in the example that show that the viewport names can now be dropped from the routing instructions.
Reserve viewports for components using used-by
used-by
The used-by
attribute on the au-viewport
component can be thought of as (almost) the parallel of the viewport
configuration option on route configuration. Using this property on a viewport, you can "reserve" a viewport for particular component(s). In other words, you are instructing the router that no other components apart from those specified can be loaded into a viewport with used-by
set.
In this example, we are instructing the router-lite to reserve the first viewport for ce-two
custom element and the reserve the second viewport for ce-one
custom element. You can see this in the live example below, by clicking the links and observing how the components are loaded into the reserved viewports.
You can reserve a viewport for more than one component. To this end, you can use comma-separated values for the used-by
attribute.
The live example below shows this in action
Although the used-by
attribute feels like a markup alternative of the viewport
configuration option on route configuration, there is a subtle difference. Having the used-by
property on a particular viewport set to X
component, does not prevent a preceding viewport without any value for the used-by
property to load the X
component. This is shown in action in the example below.
Note how clicking the links load the components also in the first viewport without any value for the used-by
.
Specify a default component for a viewport
When no route is loaded into a viewport, a 'default' route is loaded into the viewport. For every viewport, such defaults can be configured using the default
attribute. It is optional to specify a value for this attribute and the empty string (''
) is used as the default value for this property. This explains why the route with empty path (when exists) is loaded into a viewport without the default
attribute set, as seen in the sibling viewports example.
Another path can be used to override the default value of the default
attribute. The following example shows four viewports with varied values for the default
attribute. Whereas the first viewport might be the usual viewport with empty path, the other three specifies different default values. These components are loaded into the viewport, by default when the application is started.
The example below shows this in action.
Note that default
attribute can also be bound to null
, to instruct the router-lite not to load any component into ths viewport when no component is scheduled (either by explicit instruction of implicit availability check) to be loaded into the viewport. This is useful when you have more than one viewports and you want to load the empty path (assuming it is configured) in a particular viewport. In that case, you can bind null
to the default
attribute of the other viewport. To see examples of this, please refer to the sibling viewport section.
Specify a fallback component for a viewport
If a route cannot be recognized, a fallback route is looked for and loaded (when configured) into the viewport. Such fallback can be configured using the fallback
property of the route configuration. au-viewport
also offers a similar fallback
attribute, using which a fallback component can be configured for a particular viewport. The fallback
attribute is similar to its route configuration counterpart, with only one difference. The fallback
attribute in the au-viewport
, when configured, always takes precedence over the fallback
route configuration option. This is shown in the live example below.
A function for the value of fallback
is also supported. An example looks like as follows, where the example redirects the user to NF1
component if an attempt to load a path /foo
is made. Every other attempt to load an unknown path is results loading the NF2
component.
You can also see this in action below.
Last updated