Javascript 获取url的角度更改
我有两个不同的项目 身份验证项目:将主机作为Javascript 获取url的角度更改,javascript,angular,typescript,angular6,angular-services,Javascript,Angular,Typescript,Angular6,Angular Services,我有两个不同的项目 身份验证项目:将主机作为http://localhost:4200 在这里,我将有一个登录页面,用户将输入登录的详细信息 成功登录后,用户将被引导到另一个名为Application的项目 应用程序项目:主机为http://localhost:8000 因此,如果用户成功登录,他将出现在仪表板中,如http://localhost:8000/dashboard此应用程序项目的第页 在这种情况下,一切都正常 假设用户正在输入http://localhost:8000/dashbo
http://localhost:4200
在这里,我将有一个登录页面,用户将输入登录的详细信息
成功登录后,用户将被引导到另一个名为Application
的项目
应用程序项目:主机为http://localhost:8000
因此,如果用户成功登录,他将出现在仪表板中,如http://localhost:8000/dashboard
此应用程序项目的第页
在这种情况下,一切都正常
假设用户正在输入http://localhost:8000/dashboard
然后我需要他直接重定向到http://localhost:4200
用于登录
因为没有登录,用户无法进入此项目http://localhost:8000/dashboard
直接输入。
与Auth-guard相关的东西都做好了,一切正常
我需要的是,如果用户以http://localhost:8000/users
(注意url是用户),然后他将直接进入登录页面,登录后我需要重定向到他来自的同一url
因此,场景是用户以http://localhost:8000/users
但他没有登录,因此他被重定向到登录,成功登录后,他被重定向到http://localhost:8000/
但是我需要将他重定向到http://localhost:8000/users
因为这是他来自的地方
如何获取他来自的url
我正在使用angular 7应用程序。将源url作为GET参数添加到从localhost:8000/用户到localhost:4200的重定向中。类似于localhost:4200?url=%2f用户 在登录页面上,检查参数是否已设置。如果设置为,则重定向到localhost:8000/用户,否则重定向到默认localhost:8000/仪表板 此示例演示如何重定向
let currentUrl;
// get current url, e.g. currentUrl = "/users"
// or currentUrl = window.location.pathname
window.location.href = "http://localhost:4200?url=" + encodeURIComponent(currentUrl);
请记住使用decodeURIComponent
对url进行解码
您可以使用
this.activatedRoute.queryParams.subscribe(params => {
const url = decodeURIComponent(params['url']);
console.log(url);
});
let urlParam = window.location.search.substr(1).split("&").map(function(el) {return el.split("=");}).find(function(el) {return el[0] === "url";})
let url = urlParam ? decodeURIComponent(urlParam[1]) : "";
let urlParam = window.location.search.substr(1).split("&").map(function(el) {return el.split("=");}).find(function(el) {return el[0] === "url";})
let url = urlParam ? decodeURIComponent(urlParam[1]) : "";
let urlParam = window.location.search.substr(1).split("&").map(function(el) {return el.split("=");}).find(function(el) {return el[0] === "url";})
let url = urlParam ? decodeURIComponent(urlParam[1]) : "";
在Auth Guard中,您可以保存以前的URL;如果cx未登录,请将cx重定向到登录页面。一旦用户成功登录,请检查服务中是否有任何setRedirect URL,如果是,则重定向到该页面,否则重定向到默认页面
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
const url: string = state.url;
if (this.loginService.isUserLoggedIn()) {
return true;
}
this.loginService.setRedirectUrl(url);
this.loginService.redirectToLogin();
return false;
}
loginService.ts
setRedirectUrl(url: string): void {
localStorage.setItem('state', url);
}
用户登录后,检查本地存储中是否有任何状态密钥,如果有,重定向到该URL,否则重定向到默认页面。希望这有帮助您可以创建一个角度服务,例如重定向服务,它有一个布尔标志 和重定向url 当用户输入url时 在ngOnInit方法中,您将标志设置为true,并使用路由器模块获取url 在登录页面上,检查标志并在登录后重定向这就是我们要做的: 在狱中
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.isAuthorized) {
return true;
}
// not logged in so redirect to login page with the return url
console.log("navigating to login page with return url: "+ state.url);
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
然后在登录中,我们将它们重定向回returnUrl我已经说过,身份验证是不同的项目,所以。。对于重定向,我们将给出
window.location.href=http://localhost:4200';
(此url用于登录)和nothis.router.navigate
在这种情况下如何在window.location.href
中传递返回url?/这不是仍然是相同的过程吗?1) 检查授权2)如果未在登录应用程序中授权this.location.href=(loginurl)?returnUrl=state.url 3)从querystring获取url并使用this.location.href=…导航。我在问题中提到,没有与两个项目的链接。。身份验证和应用程序是两个不同的项目。。仅将登录的用户详细信息存储在cookie中,并使用窗口进行重定向。location.href
注释不用于扩展讨论;这段对话已经结束。