Dynamically add a router-view


Hi guys! Do you see a way of dynamically (and conditionally) adding a router view an element?
My use case is the following:
My application has pages: custom elements whose viewmodel extend a base class with a few page-related features.
It also has components: custom elements that extend a base class with a few non-page-related features.
In all my pages templates, I always add a router view at the bottom <router-view></router-view>.
I would like to automate this part in my BasePage class so it cannot be forgotten or misspelled or whatever.

I thought about using templatingengine.enhance() but I stumbled upon ViewFactory.compile and I’m not sure which would be more appropriate to the task.

Any idea?


Have you taken a look at layout? (just pointing out the obvious)


Doesn’t work.

@gama410 I don’t have much to add. I’m sure enhance() would work fine. I’m not sure the cost of all that heavy lifting on the internals is worth the price of preventing <router-view> from being missing or misspelled. But that’s your call.


Thanks @khuongduybui
That was actually a very simple idea. Unfortunately, it doesn’t work because it seems the view is completely created before being added to the layout.
So when I moved the router-view to the layout, aurelia complained that it couldn’t find a routerview to load a child view…