简体   繁体   English

Angular2自动重定向到其他路径

[英]Angular2 is automatically redirecting to a different path

Update I have an error that makes it load wrong something with an external file that doesn't load. 更新我有一个错误,使它无法加载的外部文件加载了错误的东西。

So I have this problem when I go from localhost:3000/afdelingen/1 to localhost:3000/patienten/1 . 所以当我从localhost:3000 / afdelingen / 1转到localhost:3000 / patienten / 1时遇到了这个问题。 The url instantly jumps back to localhost:3000/afdelingen/1 but the correct component is still loaded . 该URL立即跳回 localhost:3000 / afdelingen / 1,但仍加载正确的组件 I can also tell you that the ngOnInit is called twice but the onClick is only called once so this happens because the url changes. 我还可以告诉您ngOnInit被调用两次,onClick仅被调用一次,因此发生这种情况是因为url发生了变化。 and that it works correctly when I go from localhost:3000/dashboard to localhost:3000/afdelingen/1. 当我从localhost:3000 / dashboard转到localhost:3000 / afdelingen / 1时,它可以正常工作。

Here is my app.module.ts (Sorry for dutch in the code) 这是我的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 { }

Component that makes the call: 进行调用的组件:

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]);
  }
}

Component that it correctly loads: 正确加载的组件:

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);
  }

}

If you need anything else just tell me. 如果您还有其他需要,请告诉我。

This typically means there is an error in the component you are calling. 这通常意味着您正在调用的组件中存在错误。 Best thing to troubleshoot is comment out all your code in the patienten nginit so its just an empty component. 最好的解决方法是将耐心的nginit中的所有代码注释掉,因此它只是一个空组件。 Then try and load it. 然后尝试加载它。 If it does load there is a problem with your nginit. 如果确实加载,则您的nginit存在问题。

Might want to also comment out that route params. 可能还想注释掉该路线参数。

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

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