0

i'm working on a very basic barcodescanner and having some issues rooting to a view from a controller. In my app.component.ts in the ngOnInit() function I check whether a local IP has been saved in Application-settings and if it hasn't been set the app should root to a settings view where the IP can be set. When I use [nsRouterLink] to navigate to the settings view it works fine but I don't seem to be able to do this programatically... Any idea what i'm doing wrong?

app.component.ts:

    import { Component, OnInit } from "@angular/core";
    import { Router } from '@angular/router';
    import { RestService } from './services/rest.service';
    import { AppSettingsService } from './services/app-settings.service';


    @Component({
        selector: "main",
        template : "<page-router-outlet></page-router-outlet>"
    })
    export class AppComponent implements OnInit {

        constructor(private restService: RestService, private appSettingsService: AppSettingsService, private router: Router) {

        }

        ngOnInit() {

      let ip = this.appSettingsService.findLocalIP();
      if (ip !== null) {
          this.restService.init(ip);
      } else if (ip === null) {
          console.log("ip: " + ip);
          this.router.navigate(['settings']);
      }

    }

    }

app.routing.ts:

import { AppComponent } from './app.component';
import { HomeComponent } from './pages/home/home.component';
import { CheckBarcodesComponent } from './pages/check-barcodes/check-barcodes.component';
import { SettingsComponent } from './pages/settings/settings.component';

export const routes = [
    {path: "", component: HomeComponent},
        {path: "checkBarcodes", component: CheckBarcodesComponent},
            {path: "settings", component: SettingsComponent}
];

export const navigatableComponents = [
HomeComponent,
CheckBarcodesComponent,
SettingsComponent
];
2
  • Does your console.log("ip: " + ip); get called? Commented Jan 9, 2017 at 8:34
  • @Peza yes it gets logged as null every time. Commented Jan 9, 2017 at 9:01

2 Answers 2

1

Complete edit of my question. I think I found your problem now. The problem most likely is that you are trying to navigate from AppComponent, where you have your router-outlets. Therefore ngOnInit is fired, but not navigating.

Well now... OP found just now out the solution, as I was writing this post... :D

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

7 Comments

Oh okay, I misunderstood a bit, so you get to the point where you should navigate, but that doesn't work? Could we see your routing module? And tell us from where you are trying to route to settings :)
I just added my routing component. i'm trying to navigate to settings as soon ass possible when the app starts if the IP hasn't been set. I can navigate to settings using [nsRouterLink] from xml so i'm guessing the issue isn't with the routing component...
in my app.module.ts I have the following under declarations: declarations : [ AppComponent, ...navigatableComponents ], And under imports: NativeScriptRouterModule, NativeScriptRouterModule.forRoot(routes) ],
Try changing NativeScriptRouterModule.forRoot(routes) to RouterModule.forRoot(routes) and see what happens :)
I get a Unable to find RouterModule error... very strange that nsRouterLink works but not routerExtensions.navigate...
|
0

There are some difference in route implementation between angular and nativescript. Instead of importing for angular route, you have to import from nativescript route

import { RouterExtensions, PageRoute } from "nativescript-angular/router";

on constructor

constructor(private router: RouterExtensions) {
}

then try this

 this.router.navigate(['settings']);

1 Comment

I had it like that before and it didn't work. Just changed it to use RouterExtensions again but it still doesn't navigate...

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.