[英]Angular redirects to weird url string
I have [AuthGuard]
for /profile/personal-data
which watching if the user has logged or not.我有用于
/profile/personal-data
的[AuthGuard]
来观察用户是否登录。 And after login()
I do this.router.navigates['profile/persnal-data']
and it works correct.在
login()
之后我这样做this.router.navigates['profile/persnal-data']
并且它工作正常。 But when I'm not logged in and try to put in browser /profile/person
then app redirects me to ?path=%2Fprofile%2Fpersonal-data
.但是,当我没有登录并尝试输入浏览器
/profile/person
时,应用程序会将我重定向到?path=%2Fprofile%2Fpersonal-data
。 I don't know what am I doing wrong?我不知道我做错了什么?
route.config路由配置
const routes: Routes = [
{
path: '',
component: ProfileComponent,
children: [
{ path: '', redirectTo: 'personal-data', pathMatch: 'full' },
{ path: 'personal-data', component: PersonalDataComponent, canActivate: [AuthGuard] },
]
}
];
Gurard.ts古拉德.ts
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthentificationService } from '../services/authentification.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
private router: Router,
private authentificationService: AuthentificationService,
) { }
isEmpty(obj): boolean {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return Observable.create(observer => {
if (
localStorage.getItem('access-token-1cb-portal') != null &&
localStorage.getItem('expires-at-access-1cb-portal') != null &&
localStorage.getItem('refresh-token-1cb-portal') != null &&
localStorage.getItem('expires-at-refresh-1cb-portal') != null
) {
observer.next(true);
observer.complete();
return;
} else {
this.router.navigate(['/'], {
queryParams: {
path: state.url
}
});
observer.next(false);
observer.complete();
return;
}
});
}
}
UPD 1更新 1
I have updated my Guard to simple我已将我的 Guard 更新为 simple
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request);
}
It still redirect's me to main page with strange url http://localhost:4200/?path=%2Fprofile%2Fpersonal-data
but when I remove canActivate: [AuthGuard]
from my route.config
for profile
route,and it workes and redirect to the right page.它仍然用奇怪的 url
http://localhost:4200/?path=%2Fprofile%2Fpersonal-data
将我重定向到主页但是当我从我的route.config
中删除canActivate: [AuthGuard]
用于profile
路由时,它可以工作并重定向到正确的页面。 What is going wrong, why the guard make huge sense for this behaviour?出了什么问题,为什么守卫对这种行为很有意义?
I guess that the pathMatch: 'full' (in the routes constant) prevents you to go to /profile/personal-data when typing /profile.person.我猜 pathMatch: 'full' (在路由常量中)会阻止您在输入 /profile.person 时将 go 转到 /profile/personal-data。 Try adding { path: "**", redirectTo: "personal-data" } at the bottom.
尝试在底部添加 { path: "**", redirectTo: "personal-data" }。
Sorry guyes, it was my stupid mistake my Guard was redirecting to weird string due to code that I have written in my guard a long time ago.对不起,伙计们,由于我很久以前在我的警卫中编写的代码,我的警卫重定向到奇怪的字符串是我的愚蠢错误。 Sorry I have missed this code
对不起,我错过了这段代码
return Observable.create(observer => {
if (
localStorage.getItem('access-token-1cb-portal') != null &&
localStorage.getItem('expires-at-access-1cb-portal') != null &&
localStorage.getItem('refresh-token-1cb-portal') != null &&
localStorage.getItem('expires-at-refresh-1cb-portal') != null
) {
observer.next(true);
observer.complete();
return;
} else {
this.router.navigate(['/'], {
queryParams: {
path: state.url // that is the reason
}
});
observer.next(false);
observer.complete();
return;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.