2016-05-13 66 views
1

我试图阻止回去在我的本机应用程序使用NativeScript与angular2,我试图按照文件:: https://docs.nativescript.org/core-concepts/navigation#example-8--prevent-user-from-going-back-using-clearhistory-property,一旦我跑我的应用程序,我得到一个错误:Nativescript角2导航最上面

JS: Angular 2 is running in the development mode. Call enableProdMode() to enabl 
     e the production mode. 
     JS: ANGULAR BOOTSTRAP DONE. 
     JS: EXCEPTION: Error: Failed to load Page from entry.moduleName: ListPage in [nu 
     ll] 
     JS: ORIGINAL EXCEPTION: Error: Failed to load Page from entry.moduleName: ListPa 
     ge 

这是我的源代码:

import {Component, ElementRef, OnInit, ViewChild} from "angular2/core"; 
import {TextField} from "ui/text-field"; 
import {Frame} from "ui/frame"; 
import {Grocery} from "../../shared/grocery/grocery"; 
import {GroceryListService} from "../../shared/grocery/grocery-list.service"; 
import frameModule = require("ui/frame"); 

var socialShare = require("nativescript-social-share"); 

@Component({ 
    selector: "list", 
    templateUrl: "pages/list/list.html", 
    styleUrls: ["pages/list/list-common.css", "pages/list/list.css"], 
    providers: [GroceryListService] 
}) 
export class ListPage implements OnInit { 
    groceryList: Array<Grocery> = []; 
    grocery: string = ""; 
    isLoading = false; 
    listLoaded = false; 
    topmost = frameModule.topmost(); 




    @ViewChild("groceryTextField") groceryTextField: ElementRef; 

    constructor(private _groceryListService: GroceryListService) { } 

    share() { 
    let list = []; 
    for (let i = 0, size = this.groceryList.length; i < size; i++) { 
     list.push(this.groceryList[i].name); 
    } 
    let listString = list.join(", ").trim(); 
    socialShare.shareText(listString); 
    } 


    delete(grocery: Grocery) { 
    this._groceryListService.delete(grocery.id) 
     .subscribe(() => { 
     var index = this.groceryList.indexOf(grocery); 
     this.groceryList.splice(index, 1); 
     }) 
    } 

    add() { 
    if (this.grocery.trim() === "") { 
     alert("Enter a grocery item"); 
     return; 
    } 

    // Dismiss the keyboard 
    let textField = <TextField>this.groceryTextField.nativeElement; 
    textField.dismissSoftInput(); 

    this._groceryListService.add(this.grocery) 
     .subscribe(
     groceryObject => { 
     this.groceryList.unshift(groceryObject); 
     this.grocery = ""; 
     }, 
    () => { 
     alert({ 
      message: "An error occurred while adding an item to your list.", 
      okButtonText: "OK" 
     }); 
     this.grocery = ""; 
     } 
    ) 
    } 

    ngOnInit() { 
    this.isLoading = true; 
    this._groceryListService.load() 
     .subscribe(loadedGroceries => { 
     loadedGroceries.forEach((groceryObject) => { 
     this.groceryList.unshift(groceryObject); 
     }); 
     this.isLoading = false; 
     this.listLoaded = true; 
    }); 
    this.topmost.navigate({ 
     moduleName: "ListPage", 
     clearHistory: true 
    }); 

    } 
} 

我一直在谷歌上搜索,并不能得到我想要的东西。在这种情况下,我使用的教程NativeScript具有角JS 2 :) 谢谢

回答

0

为了获得Page对象转换成你需要把它注入到组件构造的角度成分:

import { Component, Inject, OnInit } from "angular2/core"; 
import { Page } from "ui/page"; 
... 
export class ListPage implements OnInit { 
    constructor(@Inject(Page) private page: Page) { 
    } 
} 

后您不应该在{N}中通常使用Page对象。

+0

的部分解决方案,您好我使用该代码为防止回去,当应用程序enetring这个模块..它正确? –

1

给出的示例通过使用新RouterExtensions类,可以向后禁用每个页面导航。您可以在官方文档中阅读有关RouterExtensions的更多信息。

可以查看示例代码here

而且我也有这个问题,并在stackoverflow post