简体   繁体   English

路由器导航在订阅内不起作用(角度)

[英]router navigate not working inside a subscribe (angular)

I'm facing the following problem:我面临以下问题:

Im call my getByUsernameandPassword method and I recieve a response, but when I try to redirect the user to the /zeiterfassung route it just shows me the same /login page.. I wrote a console.log() to check if its entering in the res, and it does.. can someone please tell me what I am doing wrong?我调用我的 getByUsernameandPassword 方法并收到响应,但是当我尝试将用户重定向到 /zeiterfassung 路由时,它只显示相同的 /login 页面。我写了一个 console.log() 来检查它是否进入res,确实如此..有人可以告诉我我做错了什么吗?

Component:零件:

  public async anmelden() {
    this.loading = true;
    this._benutzerservice.getByUsernameAndPassword(this.benutzerFormControl.value, this.passwortFormControl.value)pipe(first()).subscribe(
      res =>  {
        this._router.navigate(['/zeiterfassung']);
      },
      err => {
        console.log("a");
      });

Try routing outside the subscription,尝试在订阅之外路由,

public async anmelden() {
    this.loading = true;
    this._benutzerservice.getByUsernameAndPassword(this.benutzerFormControl.value, this.passwortFormControl.value) pipe(first()).subscribe(
        res => {
            this.redirect('/zeiterfassung');
        },
        err => {
            console.log("a");
        });
}

redirect(path) {
     this.router.navigate([path]) 
}

by default navigate method will try to navigate to yourdomain.com/zeiterfassung默认情况下,导航方法将尝试导航到 yourdomain.com/zeiterfassung
by following way it will navigate to current URL eg yourcurenturl/zeiterfassung通过以下方式,它将导航到当前的 URL 例如 yourcurenturl/zeiterfassung

import { ActivatedRoute } from '@angular/router';

constructor(private route :ActivatedRoute) 

public async anmelden() {
    this.loading = true;
    this._benutzerservice.getByUsernameAndPassword(this.benutzerFormControl.value, this.passwortFormControl.value)pipe(first()).subscribe(
      res =>  {
        this._router.navigate(['zeiterfassung'],{relativeTo:this.route});
      },
      err => {
        console.log("a");
      });

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

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