NativeScript Angular:错误错误:未捕获(在承诺中):错误:无法匹配任何路由。网址细分:' addpatient'

时间:2018-03-14 09:24:52

标签: angular typescript routing nativescript

所以我只想在home.component.ts中使用fab按钮来路由到我的addpatient.component。使用标签脚手架,并将我的addpatient文件夹放在主文件夹中。这就是我的目录:

app directory
rest of app directory

我真的很陌生,所以我觉得我只是错过了一些非常简单的事情

home.component.ts

import { Component, OnInit, ViewContainerRef, NgModule } from 
"@angular/core";
var Sqlite = require("nativescript-sqlite");
import { registerElement } from "nativescript-angular/element-registry";
import { Fab } from "nativescript-floatingactionbutton";

import { AddPatientComponent } from "./addpatient/addpatient.component";

import { Router } from "@angular/router";
registerElement("Fab", () => Fab);


@Component({
    selector: "Home",
    moduleId: module.id,
    templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {
    constructor(private router: Router){}

    public redirectAddPatient(){
        this.router.navigate(["addpatient]);
    }
}

home.component.html

<FAB row="1" (tap)="redirectAddPatient()" icon="res://ic_plus_white" rippleColor="#f1f1f1" class="fab-button"></FAB>

addpatient.component.ts

import { Component } from "@angular/core";
import { Patient } from "./../../objects/patient.module"
import { NativeScriptRouterModule } from "nativescript-angular/router";

@Component({
    selector: "addpatient",
    moduleId: module.id,
    templateUrl: "./addpatient.component.html"
})

export class AddPatientComponent {
    public lastname: string;

    ngOnInit(): void{
        let patient = new Patient();

        patient.setLastName(this.lastname);
        console.log("lastname: ", patient.getLastName());
    }
}

app.routing.ts

import { AddPatientComponent } from 
"./tabs/home/addpatient/addpatient.component";
import { HomeComponent } from "./tabs/home/home.component";

export const appRoutes: any = [
    {path: "", component: HomeComponent},
    {path: "addpatient", component: AddPatientComponent}
];

export const appComponents: any = [
    HomeComponent,
    AddPatientComponent
];

main.ts

import { platformNativeScriptDynamic } from "nativescript-angular/platform";

import { AppModule } from "./app.module";

import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { appComponents, appRoutes } from "./app.routing";

@NgModule({
    declarations: [AppComponent, ...appComponents],
    bootstrap: [AppComponent],
    imports: [
        NativeScriptRouterModule,
        NativeScriptRouterModule.forRoot(appRoutes)
    ],
})

class AppComponentModule {

}

platformNativeScriptDynamic().bootstrapModule(AppModule);

1 个答案:

答案 0 :(得分:0)

首先,您的HomeComponent的redirectAddPatient()中缺少引号:this.router.navigate(["addpatient*here*]); 其次,你引导的模块是AppModule,而不是AppComponentModule。我建议将AppComponentModule的声明和导入添加到AppModule并完全删除AppComponentModule,因为它没有任何意义(据我所知)。