Angular 使用返回url 9登录后无法匹配任何路由
我正在使用Angular current版本,我正在构建这个应用程序进行测试(fakebackend等)。一切似乎都在正确的位置,我登录了,凭据存储在一个数组中,但当我尝试登录时,我遇到了一些问题。以下是相关代码: 路线tsAngular 使用返回url 9登录后无法匹配任何路由,angular,authentication,redirect,logging,Angular,Authentication,Redirect,Logging,我正在使用Angular current版本,我正在构建这个应用程序进行测试(fakebackend等)。一切似乎都在正确的位置,我登录了,凭据存储在一个数组中,但当我尝试登录时,我遇到了一些问题。以下是相关代码: 路线ts {path:'home', component: HomeComponent}, {path:'login', component:LoginComponent}, {path:'signup', component: SignupComponent}, {
{path:'home', component: HomeComponent},
{path:'login', component:LoginComponent},
{path:'signup', component: SignupComponent},
{path:'detail/:id', component: UserDetailComponent},
{path:'', redirectTo:'/login', pathMatch:'full'},
登录TS文件
ngOnInit(): void {
this.form = this.fb.group({
userName: ['', Validators.required],
password: [null, Validators.required]
})
}
get userName(){return this.form.get('userName')}
get password(){return this.form.get('password')}
onSubmit() {
if (this.form.invalid)
return
let credentials = {
userName: this.userName.value,
password: this.password.value
}
this.authService.login(credentials)
.pipe(first())
.subscribe(data => {
let returnUrl = this.route.snapshot.queryParamMap.get('returnUrl');
this.router.navigate([ returnUrl ||'/home '])
})
}
当我尝试使用有效凭据(存储在localStorage中)登录时,出现以下错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home%20'
Error: Cannot match any routes. URL Segment: 'home%20'
at ApplyRedirects.noMatchError (router.js:4396)
所以,我想我一定是做错了什么,但我不知道是什么。有人能帮我吗?您应该删除路由链接中的空格
this.router.navigate([ returnUrl ||'/home ']) //remove the space
改为:
this.router.navigate([ returnUrl ||'/home'])
就是这样,有了弦,每一次的细心都不会太多;)非常感谢。