android - 在 NativeScript Angular 中导航嵌套路由
问题描述
在我的 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 失败 Error: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference。
如果我只是删除<page-router-outlet>
SubjectComponent. 我知道我在嵌套这些路由时搞砸了,但错误并没有抛出任何具体的东西,所以我无法弄清楚。
Playground Sample:打开 Playground 示例,然后单击“Open Subject”按钮。当您在主题页面上时,单击底部的“仪表板”按钮。你会得到错误。游乐场链接: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>
user.component.ts 中的 openDashboard() 方法。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;
}
主题.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=" Discussion"></Label>
<Label class="tab-indicator"></Label>
</StackLayout>
<StackLayout (tap)="changeTab('assignment')" class="tab">
<Label class="tab-label" text=" 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>
主题组件.ts
import { RouterExtensions } from "nativescript-angular/router";
constructor(private routerExtensions: RouterExtensions) {}
changeTab(tabName){
this.tabMode = tabName;
this.routerExtensions.navigateByUrl('user/subject/' + tabName);
}
解决方案
我怀疑问题是你用 ScrollView 包装了你的框架(页面路由器出口)。由于各种原因,这不是一个好习惯,并且该应用程序似乎因此而崩溃。在页面内移动 ScrollView 可以解决此问题。
推荐阅读
- python - 用python编写的魔方计时器?
- redhawksdr - USRP 在 RedhawkSDR 中不可见
- typescript - 如何创建从 Cloud Storage 返回对象列表的 Google Cloud 函数
- html - 使用 css/html 模拟图像中的垂直和水平标题
- r - 多个值的散点图 R
- excel - 如何将 Word 文档打印为封面?
- ruby-on-rails - 在 Rails 中设置 session_id cookie SameSite 属性
- azure-devops - 在 Azure DevOps 中运行管道后,生成将去哪里?
- node.js - 导入的 Sequelize 连接在一个文件中正常工作,但在另一个文件中不能正常工作
- c# - ServiceStack 的 HttpResult 类没有正确格式化 CSV 文件