在没有 authguard 的情况下,在 Angular 7 中禁用 url 更改导航或直接页面访问? [英] Disable url change navigation or direct page access in Angular 7 without authguard?

查看:27
本文介绍了在没有 authguard 的情况下,在 Angular 7 中禁用 url 更改导航或直接页面访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法或建议,如果用户更改 url 以访问这样的不同页面

Hi I am looking for a method or suggestions that if user change url to access to different page like this

它会限制用户和用户重定向到同一页面,有没有没有 Authguard 或不使用任何模块的方法?

it will restrict the user and user redirect to same page, is there any method to do without Authguard or without using any Modules ?

我的 stackblitz 项目不起作用

my stackblitz project which is not working

这里

推荐答案

当用户手动更改他的 url 时,您的应用程序将被卸载并再次加载.你可以用 javascript 做一些 hack,比如在卸载时写 url,然后在加载时返回它

when user changes his url by hand your application will be unloaded and loaded again. you can do some hacks with javascript like write url on unload, and then return it back on load

window.addEventListener('beforeunload', function(e) {
  localStorage.setItem("appUnloading", JSON.stringify({time: Date.now(), url: location.pathname}));
};
.....
export class GuardForAllRoutes {
  constructor(private router: Router) {}
  canActivate() {
    const appUnloadingState = localStorage.getItem("appUnloading");
    let time, url;
    if(appUnloadingState) {
      {time, url} = JSON.parse(appUnloadingState);
    }
     if(Date.now() - time < 10000) { //let say your page will be loaded for no more that 10 seconds
        return this.router.parse(url); // this won't work on old angular versions. use router.navigate instead
     }

  }
}

这篇关于在没有 authguard 的情况下,在 Angular 7 中禁用 url 更改导航或直接页面访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆