第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

路線導(dǎo)航在角度 9 中不起作用

路線導(dǎo)航在角度 9 中不起作用

慕慕森 2022-11-27 17:16:05
我正在我的角度應(yīng)用程序中處理登錄和主頁組件。登錄服務(wù)正在驗證用戶的用戶名和密碼。成功登錄后,它應(yīng)該將用戶重定向到主頁。但是路由器重定向無法正常工作。登錄組件import { Router } from '@angular/router';import { Component, OnInit } from '@angular/core';import { FormControl, FormGroup, Validators } from '@angular/forms';import { User } from 'src/app/shared/models/user';import { AuthService } from '../services/auth.service';@Component({  selector: 'app-login',  templateUrl: './login.component.html'})export class LoginComponent implements OnInit {  userName: FormControl;  password: FormControl;  loginFormGroup: FormGroup;  loginError: boolean;  errorMessage: string;  messageString: string;  constructor(private router: Router, private authService: AuthService) { }  ngOnInit() {    this.userName = new FormControl("", [Validators.required]);    this.password = new FormControl("", [Validators.required]);    this.loginFormGroup = new FormGroup({      userName: this.userName,      password: this.password    });  }  login() {    this.loginError = false;    if (this.loginFormGroup.valid) {      let user: User = new User();      user.userId = this.userName.value;      user.password = this.password.value;      this.authService.login(user)        .subscribe(res =>         {          console.log(res)          this.router.navigate(["home"])          console.log("res")        },        error =>        {          console.log(error)        });    }  }}和登錄服務(wù)import { Injectable } from "@angular/core";import { Router } from "@angular/router";import { Observable } from "rxjs";import { share } from "rxjs/operators";import { environment} from 'src/environments/environment';import { HttpClient, HttpParams, HttpHeaders} from "@angular/common/http";import { User } from 'src/app/shared/models/user';@Injectable({ providedIn: "root" })export class AuthService {  user: User;  resp=401;  get userDetail(): User {    return this.user;  }  constructor(    private router: Router,    private http: HttpClient  ) {  }
查看完整描述

2 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗 獲得超4個贊

嘗試這個。

 this.router.navigateByUrl('/home');


查看完整回答
反對 回復(fù) 2022-11-27
?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗 獲得超2個贊

用以下代碼替換您的代碼,這應(yīng)該可以工作。


 this.loginError = false;

    if (this.loginFormGroup.valid) {

      let user: User = new User();

      user.userId = this.userName.value;

      user.password = this.password.value;

      this.authService.login(user)

        .subscribe(res => 

        {

          console.log(res)

          this.router.navigate(["/home"])

          console.log("res")

        },

        error =>

        {

          console.log(error)

        });

    }

  }

編輯:


請用以下代碼替換您的 ngModule:


@NgModule({

  imports: [

    RouterModule.forRoot(routes, {

      useHash: true,

    }),

  ],

  exports: [RouterModule],

})


查看完整回答
反對 回復(fù) 2022-11-27
  • 2 回答
  • 0 關(guān)注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號