繁体   English   中英

Angular2自动重定向到其他路径

[英]Angular2 is automatically redirecting to a different path

更新我有一个错误,使它无法加载的外部文件加载了错误的东西。

所以当我从localhost:3000 / afdelingen / 1转到localhost:3000 / patienten / 1时遇到了这个问题。 该URL立即跳回 localhost:3000 / afdelingen / 1,但仍加载正确的组件 我还可以告诉您ngOnInit被调用两次,onClick仅被调用一次,因此发生这种情况是因为url发生了变化。 当我从localhost:3000 / dashboard转到localhost:3000 / afdelingen / 1时,它可以正常工作。

这是我的app.module.ts (对不起,代码中的荷兰语)

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import {AfdelingComponent} from "./afdeling/afdeling.component";
import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import {DashboardComponent} from "./dashboard/dashboard.component";
import {ToolbarComponent} from "./toolbar/toolbar.component";
import {AfdelingDetailComponent} from "./afdeling-detail/afdeling-detail.component";
import {Routes, RouterModule} from "@angular/router";
import {AfdelingService} from "./services/afdeling.service";
import {KamerComponent} from "./kamers/kamer.component";
import {KamerService} from "./services/kamers.service";
import {PatientViewComponent} from "./patienten/patient-view.component";
import {PatientService} from "./services/patienten.service";

const appRoutes: Routes = [
  { path: 'dashboard', component: DashboardComponent },
  { path: 'afdelingen/:id', component: AfdelingDetailComponent },
  { path: 'patienten/:id', component: PatientViewComponent },
  { path: '', component: DashboardComponent },
  { path: '**', component: DashboardComponent }
];

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(appRoutes)],
  declarations: [
    AppComponent,
    AfdelingComponent,
    DashboardComponent,
    ToolbarComponent,
    AfdelingDetailComponent,
    KamerComponent,
    PatientViewComponent],
  providers: [
    AfdelingService,
    KamerService,
    PatientService
  ],
  bootstrap: [
    AppComponent ]
})
export class AppModule { }

进行调用的组件:

import {Component, OnInit} from "@angular/core";
import {Kamer} from "../kamers/kamer";
import {KamerService} from "../services/kamers.service";
import {ActivatedRoute, Params, Router} from "@angular/router";


@Component({
  selector: 'afdeling-detail',
  templateUrl: './app/afdeling-detail/afdeling-detail.component.html',
  styleUrls: ['./app/afdeling-detail/afdeling-detail.component.css']

})

export class AfdelingDetailComponent implements OnInit{
  private afdelingId:number;
  private kamers:Kamer[] = [];

  private kamerNr = true;
  private patientView= false;
  private nextTreatmentTimeView= false;
  private lastTreatmentView= false;
  private sanitairView = false;
  private salonView= false;
  private childRoomView = false;

  private error:boolean;

  constructor(private kamerService:KamerService, private route: ActivatedRoute,private router:Router) {}

  ngOnInit() {

    this.route.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.kamerService.getKamers(+params['id'])) //switchmap zal eventuele openstaande calls als gevolg van snelle id wijzigingen door de user automatisch cancellen
      .subscribe((kamers:Kamer[]) => this.kamers = kamers);

    this.route.params.subscribe((params: Params) => {
      this.afdelingId = params["id"];
    });
  }

  public goBack():void{
    this.router.navigate(['dashboard']);
  }

  public onClick(patient:number):void {
    console.log("onClick");
    this.router.navigate(['patienten', patient]);
  }
}

正确加载的组件:

import {Component, OnInit} from "@angular/core";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {Patient} from "./patient";
import {PatientService} from "../services/patienten.service";
import {KamerService} from "../services/kamers.service";

@Component({
  selector: 'patient-view',
  templateUrl: './app/patienten/patient-view.component.html',
  styleUrls: ['./app/patienten/patient-view.component.css']
})

export class PatientViewComponent implements OnInit{
  private patientNr:number;
  private patient:Patient;

  constructor(private patientService:PatientService, private route: ActivatedRoute,private router:Router) {}

  ngOnInit(){
    this.route.params.subscribe((params: Params) => {
      this.patientNr = +params["id"];
      console.log("patient nr" + this.patientNr);
    });

    this.route.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.patientService.getPatient(+params['id'])) //switchmap zal eventuele openstaande calls als gevolg van snelle id wijzigingen door de user automatisch cancellen
      .subscribe((patient: Patient) => this.patient = patient);
  }

}

如果您还有其他需要,请告诉我。

这通常意味着您正在调用的组件中存在错误。 最好的解决方法是将耐心的nginit中的所有代码注释掉,因此它只是一个空组件。 然后尝试加载它。 如果确实加载,则您的nginit存在问题。

可能还想注释掉该路线参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM