【问题标题】:Nativescript angular 2 navigate topmostNativescript angular 2 导航最上面
【发布时间】:2018-07-18 00:24:32
【问题描述】:

我试图阻止在我的原生应用程序中返回使用带有 angular2 的 NativeScript,并且我尝试遵循文档 :: https://docs.nativescript.org/core-concepts/navigation#example-8--prevent-user-from-going-back-using-clearhistory-property 并且在我运行我的应用程序后出现错误:

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 和 angular2

【问题讨论】:

    标签: angular typescript nativescript


    【解决方案1】:

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

    可以查看示例代码here

    我也遇到了这个问题,在stackoverflow post有部分解决方案

    【讨论】:

      【解决方案2】:

      您可以查看NativeScript with Angular - Navigation 文章和其中给出的示例

      【讨论】:

      • 链接好像失效了
      • 太糟糕了,文档没有说明在返回时将参数从 page2 传递回 page1
      【解决方案3】:

      为了将 Page 对象放入 Angular 组件中,您需要将其注入到该组件的构造函数中:

      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 对象。

      【讨论】:

      • 嗨,当应用程序进入这个模块时,我使用该代码来防止返回。它是正确的吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2019-02-16
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多