Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 路线导航在角度9下不工作_Javascript_Angular_Angular Ui Router - Fatal编程技术网

Javascript 路线导航在角度9下不工作

Javascript 路线导航在角度9下不工作,javascript,angular,angular-ui-router,Javascript,Angular,Angular Ui Router,我正在使用angular应用程序中的登录和主页组件 登录服务正在验证用户的用户名和密码 成功登录后,它应该将用户重定向到主页 但路由器重定向无法正常工作 登录组件 从'@angular/Router'导入{Router}; 从“@angular/core”导入{Component,OnInit}; 从'@angular/forms'导入{FormControl,FormGroup,Validators}; 从'src/app/shared/models/User'导入{User}; 从“../s

我正在使用angular应用程序中的登录和主页组件

登录服务正在验证用户的用户名和密码

成功登录后,它应该将用户重定向到主页

但路由器重定向无法正常工作

登录组件

从'@angular/Router'导入{Router};
从“@angular/core”导入{Component,OnInit};
从'@angular/forms'导入{FormControl,FormGroup,Validators};
从'src/app/shared/models/User'导入{User};
从“../services/auth.service”导入{AuthService};
@组成部分({
选择器:“应用程序登录”,
templateUrl:“./login.component.html”
})
导出类LoginComponent实现OnInit{
用户名:FormControl;
密码:FormControl;
loginFormGroup:FormGroup;
逻辑错误:布尔;
错误消息:字符串;
messageString:string;
构造函数(专用路由器:路由器,专用authService:authService){}
恩戈尼尼特(){
this.userName=newformcontrol(“,[Validators.required]);
this.password=newformcontrol(“,[Validators.required]);
this.loginFormGroup=新表单组({
用户名:this.userName,
密码:这个是密码
});
}
登录(){
this.login错误=false;
if(this.loginFormGroup.valid){
let user:user=new user();
user.userId=this.userName.value;
user.password=this.password.value;
this.authService.login(用户)
.订阅(res=>
{
console.log(res)
this.router.navigate([“home”])
控制台日志(“res”)
},
错误=>
{
console.log(错误)
});
}
}
}
登录服务

从“@angular/core”导入{Injectable};
从“@angular/Router”导入{Router}”;
从“rxjs”导入{observeable};
从“rxjs/operators”导入{share};
从'src/environments/environment'导入{environment};
从“@angular/common/http”导入{HttpClient,HttpParams,HttpHeaders};
从'src/app/shared/models/User'导入{User};
@可注射({providedIn:“根”})
导出类身份验证服务{
用户:用户;
resp=401;
获取userDetail():用户{
返回此.user;
}
建造师(
专用路由器:路由器,
私有http:HttpClient
) {
}
登录(用户:用户):可观察


即使控制台正在打印成功,路由导航也不会发生,它仍在登录页面中

用下面的代码替换您的代码,这应该可以工作

 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)
        });
    }
  }
编辑:

请用以下代码替换您的NGM模块:

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
      useHash: true,
    }),
  ],
  exports: [RouterModule],
})
试试这个

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

我看到你在
/home
上有一个守卫。你把Auth令牌存放在哪里?这一次从路由器上移除authGuard,然后检查。如果它工作正常,然后去验证你的服务。移除authGuard可以工作,但子路由不可访问。我能给你的代码添加一点解释吗?为什么你的代码应该工作?当你是我们的时候对于子路由器,url导航确实包含父路由,因此它会检测到url相同,并阻止应用路由,因为您已将其配置为false。