Angular 6 如果用户使用 navigateByUrl 更改密码不起作用,则重定向到应用程序的启动组件
Angular 6 Redirect to startup component of an application if a user changed his password using navigateByUrl is not working
我目前在 angular 6 应用程序的以下 url:
http://localhost:4200/dashboard/admin/users
如果管理员更改了他的凭据,我需要重新将他重定向到登录组件。
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data == "success") {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigateByUrl('login');
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
我试过了this.router.navigateByUrl('login');
、this.router.navigateByUrl('/login');
但没有错误也没有重定向
试试这个:
constructor(..., private router: Router, ...) {}
...
this.router.navigate(['/login']);
...
从“@angular/router”导入导入{路由器};
在构造函数私有路由器中:Router
如果您将服务请求作为 json 传递,那么您的代码就像
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data) {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigate(['your link']).then(() => {
your toaster
});
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
如果您没有提前将数据解析为 json,那么您必须这样做
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data['status'] == 200) {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigate(['your link']).then(() => {
your toaster
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
如果您正在使用
import { Router } from '@angular/router'
确保你的构造函数中有它作为
constructor(private _router: Router) {}
并使用正斜杠指定路由
this._router.navigateByUrl("/login")
也许这就是你所缺少的。希望对你有帮助。
我目前在 angular 6 应用程序的以下 url:
http://localhost:4200/dashboard/admin/users
如果管理员更改了他的凭据,我需要重新将他重定向到登录组件。
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data == "success") {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigateByUrl('login');
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
我试过了this.router.navigateByUrl('login');
、this.router.navigateByUrl('/login');
但没有错误也没有重定向
试试这个:
constructor(..., private router: Router, ...) {}
...
this.router.navigate(['/login']);
...
从“@angular/router”导入导入{路由器}; 在构造函数私有路由器中:Router
如果您将服务请求作为 json 传递,那么您的代码就像
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data) {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigate(['your link']).then(() => {
your toaster
});
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
如果您没有提前将数据解析为 json,那么您必须这样做
this.auth.updateCredentials(credentials).subscribe(
(data) => {
if (data['status'] == 200) {
this.showError = false;
if (credentials['user_id'] == this.loggedUser) {
//Redirect
localStorage.setItem('jwt', '')
localStorage.setItem('user_id', '')
localStorage.setItem('user_role', '')
this.router.navigate(['your link']).then(() => {
your toaster
} else {
this.dialogRef.close();
}
}
if (data == "error") {
this.showError = true;
}
},
(error) => {
this.showError = true;
console.log(error);
}
);
如果您正在使用
import { Router } from '@angular/router'
确保你的构造函数中有它作为
constructor(private _router: Router) {}
并使用正斜杠指定路由
this._router.navigateByUrl("/login")
也许这就是你所缺少的。希望对你有帮助。