在NativeScript Angular中导航嵌套路由

问题描述 投票:0回答:1

在我的NativeScript Angular项目中,这是我的路由器模块:

export const routes = [
 {path: "user", component: UserComponent,
    children: [
        {
            path: "dashboard",
            component: DashboardComponent
        },
        {
            path: "notice",
            component: NoticeComponent
        },
        {
            path: "attendance",
            component: AttendanceComponent
        },
        {
            path: "subject",
            component: SubjectComponent,
            children: [
                {
                    path: "discussion",
                    component: DiscussionComponent
                },
                {
                    path: "assignment",
                    component: AssignmentComponent,
                    children: [
                        {
                            path: "assignment-report", component: AssignmentReportComponent
                        }
                    ]
                }
            ]
        }
    ]
  }
];

user.component.html有一个底部栏,带有仪表板,通知和考勤组件的链接,这些链接显示在UserComponent的路由器插座中。用户单击user/dashboard路径中的主题以转到主题。 SubjectComponent再次在SubjectComponent内部的路由器插座中显示了两个“讨论”和“分配”选项卡。

问题:当我进入SubjectComponent并单击底部栏中的任何链接以导航到“仪表板”,“出勤”或“通知”时,应用程序崩溃,并且出现此错误:

“主”线程上发生未捕获的异常。调用js方法onPause失败错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.graphics.Bitmap.getWidth()'

如果我只删除SubjectComponent中的<page-router-outlet>,效果很好。我知道我在嵌套这些路由时遇到了麻烦,但是该错误未抛出任何特定内容,因此我无法弄清楚。

操场样本:打开操场样本,然后单击“打开主题”按钮。当您在“主题”页面上时,单击底部的“仪表盘”按钮。您将得到错误。游乐场链接:https://play.nativescript.org/?template=play-ng&id=qGvsVF&v=11

添加一些可能与此问题有关的代码段。

user.component.html

<GridLayout style="background-color: #ffffff;" rows="*, auto">
<StackLayout class="content-container" row="0">
    <page-router-outlet></page-router-outlet>
</StackLayout>
<StackLayout class="bottom-bar" row="1">
    <StackLayout class="menu-container" horizontalAlignment="center" orientation="horizontal">
        <Image (tap)="openDashboard()" [ngStyle]="{'border-color': isHome?'#1d87c9' :'#ffffff'}" class="btn-home" [src]="menuImages[0]"></Image>
        <Image (tap)="openNotice()"  [ngClass]="{'btn-active': isNotice}" class="btn-icon" [src]="menuImages[1]"></Image>
        <Image (tap)="openAttendance()" [ngClass]="{'btn-active': isAttendance}" class="btn-icon" [src]="menuImages[2]"></Image>
    </StackLayout>
</StackLayout>

openDashboard()方法在user.component.ts中。 openNotice()和openAttendance()相似

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

constructor(private routerExtensions: RouterExtensions) {}

openDashboard(){
  this.routerExtensions.navigateByUrl('user/dashboard');
  this.menuImages = ["res://uni_full","res://notice_default","res://attendance_default"];
  this.isHome = true;
  this.isNotice = false;
  this.isAttendance = false;
}

subject.component.html

<StackLayout class="main">
<GridLayout rows="auto, *">
    <StackLayout row="0" class="subject-ribbon">
        <StackLayout class="tab-container" orientation="horizontal">
            <StackLayout (tap)="changeTab('discussion')" class="tab">
                <Label class="tab-label" text="&#xf27a;&nbsp;&nbsp;&nbsp;Discussion"></Label>
                <Label class="tab-indicator"></Label>
            </StackLayout>
            <StackLayout (tap)="changeTab('assignment')" class="tab">
                <Label class="tab-label" text="&#xf15c;&nbsp;&nbsp;&nbsp;Assignment"></Label>
                <Label class="tab-indicator"></Label>
            </StackLayout>
        </StackLayout>
    </StackLayout>
    <ScrollView (scroll)="onScroll($event)" row="1">
        <StackLayout>
            <page-router-outlet></page-router-outlet>
        </StackLayout>
    </ScrollView>
</GridLayout>

subject.component.ts

import { RouterExtensions } from "nativescript-angular/router";
constructor(private routerExtensions: RouterExtensions) {}

changeTab(tabName){
  this.tabMode = tabName;
  this.routerExtensions.navigateByUrl('user/subject/' + tabName);
}
android angular nativescript angular2-routing nativescript-angular
1个回答
0
投票

我怀疑问题是您已经用ScrollView包裹了框架(页面路由器出口)。由于种种原因,这不是一个好习惯,并且该应用似乎因此崩溃了。在页面内移动ScrollView可解决此问题。

Updated Playground

© www.soinside.com 2019 - 2024. All rights reserved.