The Nativescript RootLayout and Nativescript-Vue

adetayo adeyemi
3 min readDec 31, 2021

So, if you follow Nativescript closely, the release note for Nativescript 8 introduced a new layout, RootLayout (see release note here) and documentation on usage here

Adding all of the info on why this new Layout is great will make the post too lengthy. The focus here is on RootLayout usage in Nativescript-vue, specifically, we will focus on two common challenges:

  1. How to get a View from a SFC (the Open, close bringToFront methods of the RootLayout requires a Nativescript View instance),
  2. The use of a global mixin to provide a readily available service that can be called from any component.

To add a a bit of structure to this post, we re-produce the work done by William Juan (official demo app for Root Layout linked in the documentation) using Angular, but this time, we will use Vue.

How To get NativeScript View from a SFC

Basics first: take a quick look at the RootLayout API below

RootLayout API, source: NativeScript 8 release page

Observe the open, close, bringToFront methods require a View instance. A quick look at the usage sample on the release page

RootLayout usage Sample, source: Nativescript 8 release page

Lets see what the content of the getPopup method looks like

Manual StackLayout instantiation, before getting passed to the RootLayout “open” method

What we really want to do is have the freedom to create layout to be used in sidebar or bottom bar etc, using the SFC we’ve come to love.

To do this, we quickly remember the showModal method, found in the official Nativescript-Vue documentation

showModal method signature, source: Nativescript website

This is the exact signature we want, with the showModal method, we just pass Component (a SFC) to the showModal method, and the content magically shows in the modal. For this to work, the code that gets a Nativescript View instance out of a SFC must have been written. Well, since the Nativescript code is open source, we can check out the implementation of the showModal method

source: Nativescript-Vue code on Github

And that is it. In my next post, I will show how this method and a number of other functions are extracted into a mixin.

Thank you.

--

--

adetayo adeyemi

Java, C#, Typescript, Vuejs, Nativescript, Flutter, Traveling and meeting people: Adeyemi Adetayo Adeyinka