未加星标

Navigating A NativeScript App With The Angular 2 Router

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Unless you want a very boring single page application, you’re going to want some form of page navigation with multiple pages available. Previously I wrote a tutorial for navigating between routes in a vanilla javascript NativeScript application , but with Angular 2 in full force, it probably makes sense to demonstrate navigation with the very different Angular 2 Router component.

Anyone who has been following Angular 2 since beta knows that the navigation componentshave changed drastically in pretty much every release. Anyone who has been following NativeScript and Angular 2 knows that Telerik likes to use any and all Angular 2 in its vanilla state. This means that navigation in NativeScript Angular 2 applications has changed quite a bit over the past year. However, with Angular 2 now in general availability (GA), the Angular 2 Router is no longer beta and should no longer be changing.

We’re going to take a look at simple navigation between twoAngular 2 components in a NativeScript Android and iOS mobile application using the now stable Angular 2 Router.

To make this guide easy to follow, we’re going to create a fresh NativeScript Android and iOS project. From the Command Prompt (windows) or Terminal (Mac and linux), execute the following:

tnscreateMyProject --ng cd MyProject tnsplatformaddios tnsplatformaddandroid

Note that the --ng tag indicates an Angular 2 project, not a vanilla NativeScript project. This means we’ll be using TypeScript and Angular 2. Also note that if you’re not using a Mac with Xcode installed, you won’t be able to build for iOS.

This particular project will make use of two different navigation routes, both of which are not included in the base NativeScript template. Create the following in the app directory of your project:

mkdir -p components/page1 mkdir -p components/page2 touch components/page1/page1.ts touch components/page1/page1.html touch components/page2/page2.ts touch components/page2/page2.html

If your command line doesn’t have mkdir and touch or you don’t feel comfortable using them, go ahead and create those files and directories manually.

Let’s focus on the second page of our application first, or in other words, the page that we plan to navigate to.

Open the project’s app/components/page2/page2.ts file and include the following code:

import {Component} from "@angular/core"; @Component({ selector: "page2", templateUrl: "./components/page2/page2.html", }) export class Page2Component { public constructor() {} }

There isn’t anything special in the above TypeScript code. Essentially we are just saying that the TypeScript code is bound to the corresponding HTML file. Having created the Page2Component class is important though.

With the TypeScript out of the way for the second page, open the project’s app/components/page2/page2.html file so we can add some UI markup:

<ActionBartitle="Page 2"> <NavigationButtontext="Back"></NavigationButton> </ActionBar> <StackLayout> </StackLayout>

In the above markup, all we’re doing is creating a navigation bar with a back button and an empty layout. However, the navigation bar title is more than enough to tell us that we’ve navigated away from the first and default page.

With the second page out of the way, we can now focus on the first page which will be the default page when the application opens.

Open the project’s app/components/page1/page1.ts file and include the following TypeScript code:

import {Component} from "@angular/core"; import {Router} from "@angular/router"; @Component({ selector: "page1", templateUrl: "./components/page1/page1.html", }) export class Page1Component { public constructor(privaterouter: Router) { } public onTap() { this.router.navigate(["page2"]); } }

Without getting too far ahead of ourselves, this TypeScript file looks very similar to that of the second page. However, we’ve included an onTap method and imported the Angular 2 Router component. After having injected the Router in the constructor, we can use it to navigate to any of our available routes.

We’ve not yet defined our routes, but it is safe to assume that page2 represents our second page. We’ll get to that in a minute.

Open the project’s app/components/page1/page1.html file and include the following HTML markup:

<ActionBartitle="Page 1"> <ActionItemtext="Next" ios.position="right" (tap)="onTap()"></ActionItem> </ActionBar> <StackLayout> </StackLayout>

Notice in the above HTML we have a navigation button that triggers the onTap method when clicked? This is how the navigation is triggered.

We’re not done yet. Remember how I mentioned that we haven’t defined our available routes? We need to define those routes now.

Create an app/app.routing.ts file in your project. Within this file, the following code should exist:

import { Page1Component } from "./components/page1/page1"; import { Page2Component } from "./components/page2/page2"; exportconstappRoutes: any = [ { path: "", component: Page1Component }, { path: "page2/:name", component: Page2Component } ]; exportconstappComponents: any = [ Page1Component, Page2Component ];

Notice that we’ve imported all available pages at the top? We can establish available routes within the appRoutes array where the path represents the value used in the TypeScript navigation and the component being the corresponding component to the path.

To save us some time in the next step, we construct an array of every available component as well.

The next step would be to include this route information in the all-powerful @NgModule block found in the project’s app/main.ts file. The file would look something like this:

// this import should be first in order to load some required settings (like globals and reflect-metadata) import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform"; import { NgModule } from "@angular/core"; import { AppComponent } from "./app.component"; import { NativeScriptRouterModule } from "nativescript-angular/router"; import { appComponents, appRoutes } from "./app.routing"; @NgModule({ declarations: [AppComponent, ...appComponents], bootstrap: [AppComponent], imports: [ NativeScriptModule, NativeScriptRouterModule, NativeScriptRouterModule.forRoot(appRoutes) ], }) class AppComponentModule {} platformNativeScriptDynamic().bootstrapModule(AppComponentModule);

We’ve imported the NativeScriptRouterModule and the constant variables that we had just defined in the app/app.routing.ts file.

In the @NgModule block we declare all components found in the appComponents array in the declarations property. We also import the NativeScriptRouterModule and available routes in the imports property.

We’re almost done!

The fine step would be to determine where these routes become

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: HTMLiOSAndroidJavaScriptLinuxJavaWindows
分页:12
转载请注明
本文标题:Navigating A NativeScript App With The Angular 2 Router
本站链接:http://www.codesec.net/view/481469.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(31)