Loopbackjs 使用JWT身份验证构建环回4应用程序时TS2345错误的疑难解答

Loopbackjs 使用JWT身份验证构建环回4应用程序时TS2345错误的疑难解答,loopbackjs,jwt-auth,Loopbackjs,Jwt Auth,我正在尝试将JWT身份验证集成到我的环回4应用程序中。现在我一直在关注文档和,但是在注册身份验证策略时,我遇到了一个奇怪的构建错误 这是我的申请表。ts: export class Application extends BootMixin( ServiceMixin(RepositoryMixin(RestApplication)), ) { constructor(options?: ApplicationConfig) { super(options); this

我正在尝试将JWT身份验证集成到我的环回4应用程序中。现在我一直在关注文档和,但是在注册身份验证策略时,我遇到了一个奇怪的构建错误

这是我的申请表。ts:

export class Application extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options?: ApplicationConfig) {
    super(options);

    this.setUpBindings();

    this.component(AuthenticationComponent);
    registerAuthenticationStrategy(this, JWTAuthenticationStrategy);
    this.sequence(MySequence);

    // Set up default home page
    this.static('/', path.join(__dirname, '../public'));

    this.component(RestExplorerComponent);

    this.projectRoot = __dirname;
    // Customize @loopback/boot Booter Conventions here
    this.bootOptions = {
      controllers: {
        // Customize ControllerBooter Conventions here
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
  }

  setUpBindings(): void {
    // my bindings
  }
}
mypackage.json中的构建脚本(使用@loopback/build)

下面是我得到的错误:

npm run build =>

src/application.ts:71:33 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Context'.
  Type 'Application' is not assignable to type 'Context'.
    Property 'registry' is protected but type 'Context' is not a class derived from 'Context'.

registerAuthenticationStrategy(this, JWTAuthenticationStrategy);


我在Windows 10上使用powershell。

我也有同样的问题。 我可以通过以下命令将@loopback/context依赖项更新为最新版本来解决此问题:

npm安装--save@loopback/context

更新依赖项后,错误消失。

我找到了一个答案,您可以在其中更改注册验证策略(这是JWTAuthenticationStrategy)到
注册验证策略(如有,JWTAuthenticationStrategy)

这个办法对我有效

npm run build =>

src/application.ts:71:33 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Context'.
  Type 'Application' is not assignable to type 'Context'.
    Property 'registry' is protected but type 'Context' is not a class derived from 'Context'.

registerAuthenticationStrategy(this, JWTAuthenticationStrategy);

export class Application extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options?: ApplicationConfig) {
    super(options);

    this.setUpBindings();

    this.component(AuthenticationComponent);
    registerAuthenticationStrategy(this as any, JWTAuthenticationStrategy);
    this.sequence(MySequence);

    // Set up default home page
    this.static('/', path.join(__dirname, '../public'));

    this.component(RestExplorerComponent);

    this.projectRoot = __dirname;
    // Customize @loopback/boot Booter Conventions here
    this.bootOptions = {
      controllers: {
        // Customize ControllerBooter Conventions here
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
  }

  setUpBindings(): void {
    // my bindings
  }
}