在Angular + NativeScript中導航時傳遞對象與vanila NativeScript不同。路由完全通過角度規範實現,這意味着您需要使用它們的實現。 Angular 2的當前RC5版本使用以下導航(路由)。您需要創建與所有例如可能的應用途徑的RouterConfig
:
const ROUTES: RouterConfig = [
{ path: "", redirectTo: "/home-page", terminal: true },
{ path: "home=page", component: HomePageComponent },
{ path: "second-page", component: SecondPageComponent }
];
您還可以使用參數化路線的,並通過一些簡單的字符串參數,想象中的URI在一個網站頁面。通過這樣做,您可以例如傳遞某種「殘缺」字符串,這將幫助您在導航到的頁面上檢索所需的信息。之後,在第二頁中,您可以訂閱到ActivatedRoute
並檢索這些參數。下面是一個例子:
路線:
import { RouterConfig } from '@angular/router';
const ROUTES: RouterConfig = [
{ path: "", redirectTo: "/home-page", terminal: true },
{ path: "home=page", component: HomePageComponent },
{ path: "second-page/:firstArg/:secondArg", component: SecondPageComponent }
];
和SecondPageComponent:
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from '@angular/router';
@Component({
moduleId: module.id,
selector: "second-page",
templateUrl: "second-page.component.html",
styleUrls: ["second-page.component.css"]
})
export class SecondPageComponent implements OnInit {
constructor(private _router: Router, private _route: ActivatedRoute) {
}
ngOnInit() {
this._sub = this._route.params.subscribe(params => {
var parentTitle = params['parentTitle'];
var tappedTitle = params['tappedTitle'];
this.hasBack = false;
this._currentExample = this._exampleItemsService.getParentExampleItem(0);
});
}
你可以看看,展示這樣的導航和有趣的例子nativescript-ui-samples-angular GitHub庫。爲了運行該回購的{N}項目,您將需要free插件或pro。
什麼NG2您使用的版本,這是什麼'app.routes'樣子? 'app.modules'和這個組件發生了什麼? –
NativeScript Angular項目在導航到第二頁時只能發送一個參數。我的建議是發送param,你需要做'HTTP'請求並獲得其他數據或者在你的項目中使用全局變量來從這兩個頁面訪問'JSON'。你也可以在這裏查看我的示例項目。 - https://github.com/tsonevn/NGNavClearHistory/tree/navigate_with_params –