Create a new page nativescript
Here are the steps to create a new page in a NativeScript application:
Using the NativeScript CLI
- Open your terminal and navigate to the root directory of your NativeScript project.
- Run the following command to create a new page:
tns page create <page-name>
Replace
<page-name>
with the name you want to give your new page (e.g. "Home", "About", etc.).
For example:
tns page create home
This will create a new file called home-page.ts
in the app/pages
directory.
Using Visual Studio Code
- Open your NativeScript project in Visual Studio Code.
- Open the Command Palette by pressing
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(Mac). - Type "NativeScript: Create Page" and select the command from the dropdown list.
- Enter the name of your new page (e.g. "Home", "About", etc.) and press Enter.
This will create a new file called home-page.ts
in the app/pages
directory.
The new page file
The new page file will contain a basic page template with a Page
component and a GridLayout
component. You can customize this template to create your own page layout.
Here's an example of what the home-page.ts
file might look like:
import { Page } from 'tns-core-modules';
import { GridLayout } from 'tns-core-modules';
@Page({
moduleName: 'home-page',
title: 'Home'
})
export class HomePage {
constructor(private page: Page) {}
ngAfterViewInit() {
this.page.actionBarHidden = true;
}
}
This code defines a HomePage
component that extends the Page
component. The @Page
decorator specifies the module name and title of the page.
You can now customize this page by adding your own components, layouts, and functionality.
Adding the new page to the app
To add the new page to your app, you need to update the app-routing.module.ts
file to include the new page.
Here's an example of how you might update the app-routing.module.ts
file:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HomePage } from './pages/home-page';
@NgModule({
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomePage }
])
],
exports: [RouterModule]
})
export class AppRoutingModule {}
This code defines a route for the new page and maps it to the HomePage
component.
That's it! You've now created a new page in your NativeScript application.