Create new componenet with tns
To create a new component with TNS (TypeScript and NativeScript), you can follow these steps:
Step 1: Create a new project
Open a terminal or command prompt and run the following command to create a new NativeScript project:
tns create my-new-component
Replace my-new-component
with the name of your component.
Step 2: Create a new component file
In the my-new-component
directory, create a new file called my-new-component.ts
. This file will contain the code for your component.
Step 3: Define the component
In the my-new-component.ts
file, define your component using the @Component
decorator from the @nativescript/core
module:
import { Component } from '@nativescript/core';
@Component({
selector: 'my-new-component',
template: '<Label text="Hello, World!"></Label>'
})
export class MyNewComponent {
// Component logic goes here
}
In this example, we're defining a simple component that displays a label with the text "Hello, World!".
Step 4: Add the component to the app module
In the app.module.ts
file, add the component to the declarations
array:
import { NgModule } from '@nativescript/core';
import { MyNewComponent } from './my-new-component';
@NgModule({
declarations: [MyNewComponent],
imports: [
// Other imports...
],
providers: [
// Other providers...
],
bootstrap: [AppComponent]
})
export class AppModule {}
Step 5: Use the component in your app
In your app's main file (e.g. app.component.ts
), you can now use the new component:
import { Component } from '@nativescript/core';
import { MyNewComponent } from './my-new-component';
@Component({
selector: 'app',
template: '<StackLayout><MyNewComponent></MyNewComponent></StackLayout>'
})
export class AppComponent {
// App logic goes here
}
In this example, we're using the MyNewComponent
component in the app's main template.
Step 6: Run the app
Finally, run the app using the following command:
tns run
This will launch the app on an emulator or physical device.
That's it! You've created a new component with TNS.