我正在研究一种非常基本的条形码扫描器,并且根据控制器的视图存在一些问题。在我的ngOnInit()函数中的app.component.ts中,我检查本地IP是否已保存在应用程序设置中,如果尚未设置,则app应该根据可以设置IP的设置视图。当我使用[nsRouterLink]导航到设置视图时它工作正常,但我似乎无法以编程方式执行此操作...任何想法我做错了什么?
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
];
答案 0 :(得分:1)
完整编辑我的问题。我想我现在找到了你的问题。问题很可能是您尝试从AppComponent导航,在那里您有路由器插座。因此ngOnInit
被解雇,但没有导航。
现在好了...... OP刚刚发现解决方案,因为我正在写这篇文章......:D
答案 1 :(得分:0)
angular和nativescript之间的路由实现存在一些差异。您必须从nativescript路由
导入,而不是导入角度路由A1234567890
在构造函数
上import { RouterExtensions, PageRoute } from "nativescript-angular/router";
然后尝试这个
constructor(private router: RouterExtensions) {
}