使用router.navigate导航无法使用nativescript-tab-navigation模板

时间:2018-01-09 19:24:33

标签: angular typescript nativescript

我已正确设置所有路线并且没有出现任何错误。加载第一个选项卡时,我尝试自动导航到第二个选项卡,具体取决于它们是否已登录。

tabs-routing.module.ts文件:

import { NgModule } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

import { TabsComponent } from "./tabs.component";
import { AccountComponent } from './account/account.component';
import { AuthComponent } from './account/auth/auth.component';
import { AddComponent } from './add/add.component';
import { PantryComponent } from './pantry/pantry.component';

const routes: Routes = [
    { path: "", component: TabsComponent, children: [
      {path: 'pantry', component: PantryComponent},
      {path: 'add', component: AddComponent},
      {path: 'account', component: AccountComponent, children: [
        {path: 'auth', component: AuthComponent}
      ]}
    ]},
];

@NgModule({
    imports: [NativeScriptRouterModule.forChild(routes)],
    exports: [NativeScriptRouterModule]
})
export class TabsRoutingModule { }

pantry.component.ts文件:

import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

require( "nativescript-localstorage" );

@Component({
    selector: "pantry",
    moduleId: module.id,
    templateUrl: "./pantry.component.html"
})
export class PantryComponent implements OnInit {

    constructor(private router : Router, private realRouter: NativeScriptRouterModule) {
        /* ***********************************************************
        * Use the constructor to inject services.
        *************************************************************/
    }

    ngOnInit(): void {
        /* ***********************************************************
        * Use the "ngOnInit" handler to initialize data for the view.
        *************************************************************/

        var loggedIn = sessionStorage.getItem('loggedIn');
        if(loggedIn == 'true'){
          console.log(loggedIn);
        }
        else{
          console.log(loggedIn);
          this.router.navigate(['/tabs/add']);
        }
    }
}

这没有任何作用。为什么它不导航到AddComponent?

2 个答案:

答案 0 :(得分:0)

如果将this.router.navigate(['/tabs/add'])替换为this.router.navigate(['/add'])

,它应该有效

答案 1 :(得分:0)

我遇到了同样的问题并尝试了很多路径但没有用。有任何解决方法可以从选项卡导航到其他选项卡。 此外,手动更改标签效果很好。