0

I tried to prevent go back in my native apps use NativeScript with angular2, and I tried to follow the documentation :: https://docs.nativescript.org/core-concepts/navigation#example-8--prevent-user-from-going-back-using-clearhistory-property and I'm getting an error once I run my app :

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

This is my source code :

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
    });

  }
}

I've been googling and couldn't get what I want. In this case, I use tutorial NativeScript with angular2

3 Answers 3

1

By using new RouterExtensions class, you can disable backwards navigation per page. You can read more about RouterExtensions in the official docs.

You can view the sample code here

And I was also having this problem and have a partial solution in stackoverflow post

Sign up to request clarification or add additional context in comments.

Comments

1

You could review the NativeScript with Angular - Navigation article and the given example in it

3 Comments

The link seems to be dead
Too bad the docs says nothing about passing parameters back from page2 to page1 when back
-1

In order to get the Page object into an Angular component you need to inject it to that components constructor:

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

After that you shouldn't have issues useing that Page object as usually in {N}.

1 Comment

hi i use that code for prevent going back, when app enetring this module.. it's correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.