使用 Angular 和 TypeScript 在最新的 NativeScript 中使用参数导航
Navigate with parameters in latest NativeScript with Angular and TypeScript
我想导航到另一个带有参数的页面,但我似乎找不到对其进行很好解释的文档。我正在使用路线。这是我的路线示例。
import { RouterConfig } from '@angular/router';
import { nsProvideRouter } from 'nativescript-angular/router';
import { MainPage } from './pages/main/main.component';
import { DetailsPage } from './pages/details/details.component';
export const routes: RouterConfig = [
{ path: "", component: MainPage },
{ path: "details", component: DetailsPage }
];
export const APP_ROUTER_PROVIDERS = [
nsProvideRouter(routes, {})
];
我想使用在 MainPage 上选择的参数导航到 DetailsPage。以下是 MainPage 的摘录:
import { Page } from 'ui/page';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Entity } from '../../shared/entity/entity';
@Component({
selector: "main",
templateUrl: "pages/main/main.html",
styleUrls: ["pages/main/main-common.css", "pages/main/main.css"]
})
export /**
* MainPage
*/
class MainPage {
constructor(private _page: Page, private _router: Router) { }
onNavigate(selectedItem: Entity) {
// Can't figure out how to get selectedItem to details…
this._router.navigate(["/details"]);
};
}
已插入:下面我添加了详细信息 class。
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Entity } from '../../shared/entity/entity';
import { EntityModel } from '../../shared/entity/entity.model';
@Component({
selector: "detail",
templateUrl: "pages/detail/detail.html",
styleUrls: ["pages/detail/detail-common.css", "pages/detail/detail.css"],
providers: [EntityModel]
})
export /**
* DetailPage
*/
class DetailPage implements OnInit, OnDestroy {
entity: Entity;
private _paramSubcription: any;
constructor( private _activatedRoute: ActivatedRoute, private _entityModel: EntityModel ) { }
ngOnInit() {
console.log("detail ngOnInit was called.");
let entityName: string;
this._paramSubcription = this._activatedRoute.params.subscribe(params => entityName = params['id']);
this.entity = this._entityModel.entityNamed(entityName);
};
ngOnDestroy() {
if (this._paramSubcription) {
this._paramSubcription.unsubscribe();
};
};
}
这是详细信息的模板:
<ActionBar [title]="entity.name"></ActionBar>
<ListView [items]="entity.items">
<Template let-item="item">
<StackLayout>
<Label [text]="item.name"></Label>
<Label [text]="item.description"></Label>
</StackLayout>
</Template>
</ListView>
我发现 class 类似 NavigationContext
和方法 navigateTo
和 navigateFrom
,但我还不知道如何发送 NavigationContext
到 Page
。或者是否应该以这种方式发送。所以问题是,使用 Routing
导航到另一个页面(不是对话框)并传递参数的最佳方法是什么?
你需要说明你应该在这个路由中有参数:
export const routes: RouterConfig = [
{ path: "", component: MainPage },
{ path: "details/:id", component: DetailsPage }
];
那么,你可以这样传递:
this._router.navigate(["/details", selectedItem.id]);
在您的 DetailsPage
中,您可以使用 ActivatedRoute
服务获取参数作为可观察值。
我想导航到另一个带有参数的页面,但我似乎找不到对其进行很好解释的文档。我正在使用路线。这是我的路线示例。
import { RouterConfig } from '@angular/router';
import { nsProvideRouter } from 'nativescript-angular/router';
import { MainPage } from './pages/main/main.component';
import { DetailsPage } from './pages/details/details.component';
export const routes: RouterConfig = [
{ path: "", component: MainPage },
{ path: "details", component: DetailsPage }
];
export const APP_ROUTER_PROVIDERS = [
nsProvideRouter(routes, {})
];
我想使用在 MainPage 上选择的参数导航到 DetailsPage。以下是 MainPage 的摘录:
import { Page } from 'ui/page';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Entity } from '../../shared/entity/entity';
@Component({
selector: "main",
templateUrl: "pages/main/main.html",
styleUrls: ["pages/main/main-common.css", "pages/main/main.css"]
})
export /**
* MainPage
*/
class MainPage {
constructor(private _page: Page, private _router: Router) { }
onNavigate(selectedItem: Entity) {
// Can't figure out how to get selectedItem to details…
this._router.navigate(["/details"]);
};
}
已插入:下面我添加了详细信息 class。
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Entity } from '../../shared/entity/entity';
import { EntityModel } from '../../shared/entity/entity.model';
@Component({
selector: "detail",
templateUrl: "pages/detail/detail.html",
styleUrls: ["pages/detail/detail-common.css", "pages/detail/detail.css"],
providers: [EntityModel]
})
export /**
* DetailPage
*/
class DetailPage implements OnInit, OnDestroy {
entity: Entity;
private _paramSubcription: any;
constructor( private _activatedRoute: ActivatedRoute, private _entityModel: EntityModel ) { }
ngOnInit() {
console.log("detail ngOnInit was called.");
let entityName: string;
this._paramSubcription = this._activatedRoute.params.subscribe(params => entityName = params['id']);
this.entity = this._entityModel.entityNamed(entityName);
};
ngOnDestroy() {
if (this._paramSubcription) {
this._paramSubcription.unsubscribe();
};
};
}
这是详细信息的模板:
<ActionBar [title]="entity.name"></ActionBar>
<ListView [items]="entity.items">
<Template let-item="item">
<StackLayout>
<Label [text]="item.name"></Label>
<Label [text]="item.description"></Label>
</StackLayout>
</Template>
</ListView>
我发现 class 类似 NavigationContext
和方法 navigateTo
和 navigateFrom
,但我还不知道如何发送 NavigationContext
到 Page
。或者是否应该以这种方式发送。所以问题是,使用 Routing
导航到另一个页面(不是对话框)并传递参数的最佳方法是什么?
你需要说明你应该在这个路由中有参数:
export const routes: RouterConfig = [
{ path: "", component: MainPage },
{ path: "details/:id", component: DetailsPage }
];
那么,你可以这样传递:
this._router.navigate(["/details", selectedItem.id]);
在您的 DetailsPage
中,您可以使用 ActivatedRoute
服务获取参数作为可观察值。