当使用loadURL来加载我的网站时,我想防止用户导航到两个页面.我需要在Electron 应用程序中完成此操作.

例如,在我的应用程序中,我处于:

https://WebsiteApp.com

但我想阻止导航到该页面:

https://WebsiteApp.com/security

有什么方法可以阻止这条小路的航行吗?

pathname: '/security'

推荐答案

try 使用‘路由守卫’,它也被称为Navigation Guards,在某些框架中实现,如Angular 或Vue,在这种情况下使用Reaction路由和钩子来实现,包括TypeScrip.

无论如何,请查看this explanation,其中您可以根据身份验证级别等重定向页面,这是一个冗长的解释,但可能会对您有所帮助.

enter image description here

这个AuthGuard实现期望从AuthorizationService创建一个内部实例,以验证通过pageEnabled函数的访问.

// auth-guard.ts

import { Commands, Context, RedirectResult } from '@vaadin/router';
import { AuthorizationService } from './authorization-service';
import { PageEnabled } from './page-enabled';

export class AuthGuard implements PageEnabled {

  private authService: AuthorizationService;

  constructor() {
    this.authService = new AuthorizationService();
  }

  public async pageEnabled(context: Context, commands: Commands, pathRedirect?: string): Promise<RedirectResult | undefined> {
    const isAuthenticated = await this.authService.isAuthorized();

    if(!isAuthenticated) {
      console.warn('User not authorized', context.pathname);
     return commands.redirect(pathRedirect? pathRedirect: '/');
    }

    return undefined;
  }
}

此外,该类还需要实现定义的PageEnabled接口into /shared/auth/page-enabled.ts

//page-enabled.ts

import { Commands, Context, RedirectResult } from '@vaadin/router';

export interface PageEnabled {
  pageEnabled(
    context: Context,
    commands: Commands,
    pathRedirect?: string
  ): Promise<RedirectResult | undefined>;
}

Javascript相关问答推荐

TypScript界面中的Infer React子props

如何避免移动设备中出现虚假调整大小事件?

单击子元素时关闭父元素(JS)

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

Google图表时间轴—更改hAxis文本 colored颜色

如何找出摆线表面上y与x相交的地方?

如何调用名称在字符串中的实例方法?

如何通过将实例方法的名称传递给具有正确类型的参数的继承方法来调用实例方法?

是什么导致了这种奇怪的水平间距错误(?)当通过JavaScript将列表项元素追加到无序列表时,是否在按钮之间?

从Nextjs中的 Select 项收集值,但当单击以处理时,未发生任何情况

如何在.NET Core中将chtml文件链接到Java脚本文件?

如何在 Select 文本时停止Click事件?

TinyMCE 6导致Data:Image对象通过提供的脚本过度上载

使用插件构建包含chart.js提供程序的Angular 库?

有效路径同时显示有效路径组件和不存在的路径组件

Reaction-SWR-无更新组件

Phaserjs-创建带有层纹理的精灵层以自定义外观

FileReader()不能处理Firefox和GiB文件

Chrome上的印度时区名S有问题吗?

如何动态呈现适合未知屏幕大小的最大数量的表行?苗条的