[AngularJS] Store the entry url and redirect to entry url after Logged in

本文介绍如何使用Angular在用户登录后返回原本试图访问的页面。通过在应用启动时捕获入口URL并将其存储起来,在登录成功后将用户重定向到该URL。

For example when a outside application need to visit your app address:

https://www.example.com/#/lobby/abc

But before enter '/lobby/abc', user need to login, so the application will redirect user to 

https://www.example.com/#/login

Now what you want is after user logged in, you can redirect them to 

https://www.example.com/#/lobby/abc

Instead of other pages like:

https://www.example.com/#/lobby

 

So to solve the problem, we need to do two things

1. Store the entry url

2. After logged in, redirect user to entry url

 

1. Store the entry ur:

angular.module('app')
     // When can get pull url by using $location.absUrl()
    // Because run block only run once for the module and app module is our main application module, so this should only store our entry url
    // To reuse the entryUrl later, we store it in LoginService
    .run(($location, LoginService) => LoginService.entryUrl = $location.absUrl())

 

2. After logged in, then we can redirect user to the entry url by doing:

$location.url(LoginService.entryUrl)

 

转载于:https://www.cnblogs.com/Answer1215/p/9589788.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值