1

我正在摆弄 VaadinRouter,试图保护路线。下面你可以看到我要保护的组件的 onBeforeEnter 代码:

    authenticatedDummy = true;

    public onBeforeEnter(
        location: RouterLocation,
        commands: PreventAndRedirectCommands
      ): Promise<unknown> | RedirectResult | undefined {
        if (this.authenticatedDummy) {
          console.log('OnBeforeEnter');
    
          return new Promise(resolve => {
            setTimeout(() => {
              resolve(commands.redirect('/home'));
            }, 2000);
          });
        }
        return undefined;
      }

我不明白为什么路由器会路由我,如果authenticatedDummy是假的。当我阅读代码时,if authenticatedDummy is true, redirect me to '/home' .

这是阅读它的正确方式还是我忽略了什么?

4

1 回答 1

1

我错误地构建了脚本。它应该是:

 public onBeforeEnter(
        location: RouterLocation,
        commands: PreventAndRedirectCommands
      ): Promise<unknown> | RedirectResult | undefined {
        if (!this.isAuthorized()) {
          console.log('Guarded!');
    
          return new Promise(resolve => {
            setTimeout(() => {
              resolve(commands.redirect('/'));
            }, 2000);
          });
        }
        return undefined;
      }

它是这样工作的!

于 2021-08-28T07:01:09.580 回答