Angular Nativescript无法在惰性模块中导航

时间:2019-03-19 12:42:17

标签: angular typescript nativescript angular2-nativescript angular-router

我使用的模板: Nativescript-Tabs-Template

当我尝试使用以下方法导航到同级组件(​​均在一个惰性模块中)时:

 showItem() {
    this.routerExtensions.navigate(["details/"]);
}

(也可以执行此操作-不确定是否可以):

this.routerExtensions.navigate(["details", { outlets: { searchTab: ['details'] } }]);

我收到错误消息:

  

错误:无法匹配任何路由。网址段:“详细信息”

*但是当我使用nsRouterLink导航时,它可以工作:*

<Label text="this works" [nsRouterLink]="['/details']></Label>

App.components.html标签:

<TabView androidTabsPosition="bottom">
    <page-router-outlet
    *tabItem="{title: 'Search', iconSource: getIconSource('search')}"
    name="searchTab">
    </page-router-outlet>
</TabView>

Router.module.ts:

const routes: Routes = [
{
    path: "",
    redirectTo: "/(homeTab:home/default//browseTab:browse/default//searchTab:search/default)",
    pathMatch: "full"
},
    {
        path: "search",
        component: NSEmptyOutletComponent,
        loadChildren: "~/app/search/search.module#SearchModule",
        outlet: "searchTab"
    }
]

Search.module.ts:

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptCommonModule } from "nativescript-angular/common";

import { SearchRoutingModule } from "./search-routing.module";
import { SearchComponent } from "./search.component";
import { NgShadowModule } from 'nativescript-ng-shadow';
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { LabelMaxLinesDirective } from "../directives/label-max-lines.directive";
import { ItemDetailComponent } from "./item-detail/item-detail.component";

@NgModule({
    imports: [
        NativeScriptCommonModule,
        SearchRoutingModule,
        NgShadowModule,
        NativeScriptFormsModule,
    ],
    declarations: [
        SearchComponent,
        LabelMaxLinesDirective,
        ItemDetailComponent
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class SearchModule { }

Search.router.module.ts:

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

import { SearchComponent } from "./search.component";
import { ItemDetailComponent } from "./item-detail/item-detail.component";

const routes: Routes = [
    { path: "default", component: SearchComponent },
    { path: "details", component: ItemDetailComponent }
];

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

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

不再建议使用“ NativeScript选项卡模板”。使用NativeScript中的本教程 博客:

https://www.nativescript.org/blog/implementing-a-login-for-nativescript-apps-with-tab-based-navigation

及其在GitHub中的示例:

https://github.com/NativeScript/login-tab-navigation-ng

在此示例中,每个选项卡都有其自己的导航树,您可以独立地来回移动(就像Facebook应用程序一样)。 在标签内,每个导航都是相对的,因此请不要使用['/foo']之类的东西。

constructor(
  private route: ActivatedRoute,
  private router: RouterExtensions
) { }

goForward() {
  const navigation: ExtendedNavigationExtras = {
    relativeTo: this.route,
    transition: {
    name: 'slide'
    }
  };

  this.router.navigate(['../foo'], navigation);
}

goBack() {
  this.router.back();
}

答案 1 :(得分:0)

您是否尝试过此this.routerExtensions.navigate(["/search/details"]); 在其子路径之前添加父路径路径