Page
NativeScript applications consist of pages that represent the separate application screens.
Pages are instances of the Page
class from the Page
module. To navigate between pages, you can use the methods of the Frame
module. This article will provides guidelines for creating and accessing pages, using page events with the related event data and implementing page navigation.
Page Creation
Pages represent the separate screens of your application. Each page is an instance of the Page
class.
NativeScript provides two approaches to instantiating your pages.
Create a page in XML
You can define the UI declaration and the code for the page separately.
To apply this approach, create an XML
file for each page to hold the layout of the page. Thus your code will be in a JS
or a TS
file. The names of the XML
and the JS
or TS
file must match.
Create a page via code
To apply this approach, you need to create a function named createPage
that will return an instance of your page. NativeScript considers createPage
a factory function.
Page Reference
Getting a reference to the current Page
instance is a common scenario. The below examples are demonstrating different approaches to get Page
reference.
Reference via page events
The common navigation events and the View's loaded
and unloaded
event can be used to get a page reference.
Reference via page property
Each NativeScript view has a page
property that can be used to get a page reference.
Reference via currentPage property
Each Frame
instance has a currentPage
property that can be used to get a page reference.
Page Events
There are four page specific events triggered when a page is being navigate to and from.
-
navigatingTo
- Triggered when entering a page (the navigation has started). -
navigatedTo
- Triggered when entering a page (the navigation has ended). -
navigatingFrom
- Triggered when leaving a page (the navigation has started). -
navigatedFrom
- Triggered when leaving a page (the navigation has ended).
Forward navigation | Backward navigation |
---|---|
![]() |
![]() |
Note: The events
loaded
,unloaded
andlayoutChanged
are UI component lifecycles events and are universal for all classes that extends theView
class (includingPage
). They can be used with all NativeScript elements (e.g. layouts, buttons, UI plugins, etc.).
See Also ActionBar