Using Angular2, how to redirect to previous url before login redirect

前端 未结 4 1537
说谎
说谎 2020-12-31 03:29

Using Angular2 to create a single page app, I\'m intercepting unauthenticated user access to non-public routes in a custom RouterOutlet and redirecting them to

4条回答
  •  时光说笑
    2020-12-31 04:10

    You might find what you need in the docs for the Location class. The back() function could possibly do it for you.

    Another approach would be to subscribe to the popstate events in Location, instead. MDN has docs talking about the values you could expect to receive.

    class MyCLass {
      constructor(private location: Location) {
        location.subscribe((val) => {
            // do something with the state that's passed in
        })
      }
    }
    

    Otherwise you might want a service that tracks the changes in the Router so that you can access them.

    class MyTrackingService {
      constructor(private router: Router) {
        router.subscribe((val) => {
            // store the routes here so that you can peel them off as needed?
        })
      }
    }
    

    In this case I'm accessing the Router object and subscribing to any changes so that I could track them.

提交回复
热议问题