【问题标题】:Can't override UserAuthenticationTokenService service无法覆盖 UserAuthenticationTokenService 服务
【发布时间】:2021-02-09 16:59:36
【问题描述】:

我正在尝试为登录页面添加自定义逻辑,如here 所述,我尝试覆盖 UserAuthenticationTokenService 服务。我想为这个方法添加自定义逻辑:

loadToken(userId: string, password: string): Observable<UserToken>;

但是当我尝试导入这个类定义时,它来自

import {UserAuthenticationTokenService} from '@spartacus/core/src/auth/services/user-authentication/user-authentication-token.service';

此自动导入项目无法编译后出现错误:

./src/app/services/services.module.ts 中的错误 找不到模块:错误:无法解析“/home/gyerts/PycharmProjects/fiver/e5p-client/src”中的“@spartacus/core/src/auth/services/user-authentication/user-authentication-token.service” /应用程序/服务'

我做错了什么?

【问题讨论】:

    标签: spartacus-storefront


    【解决方案1】:

    虽然它确实没有在 Spartacus 2.x 中直接导出,但我发现它仍然可用。如果您查看编译后的 spartacus-core.d.ts 文件,您会看到 UserAuthenticationTokenService 正在以一个乱码名称导出:

    export { UserAuthenticationTokenService as ɵbj } from './src/auth/services/user-authentication/user-authentication-token.service';
    

    虽然这可能不是最好的主意,但我发现您实际上可以在自己的代码中导入它。您也可以将其重命名为原始名称以便于阅读:

    import { ɵbj as UserAuthenticationTokenService } from '@spartacus/core';
    

    我自己在我当前的项目中使用过它,它似乎工作正常:

    @Injectable({
      providedIn: 'root'
    })
    export class MyUserAuthenticationTokenService extends UserAuthenticationTokenService {}
    
    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { MyUserAuthenticationTokenService } from './services/my-user-authentication-token.service';
    import { ɵbj as UserAuthenticationTokenService } from '@spartacus/core';
    
    
    @NgModule({
      declarations: [],
      imports: [
        CommonModule,
      ],
      providers: [
        { provide: UserAuthenticationTokenService, useExisting: MyUserAuthenticationTokenService }
      ]
    })
    export class MyAuthModule {
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 Spartacus 2.x,UserAuthenticationTokenService 会出现在 @spartacus/core 中,但不幸的是它似乎没有出现在 public api 中。因此,您将无法导入它。

      如果您使用的是 Spartacus 3.x,则删除了 UserAuthenticationTokenService。 Spartacus 3.0 引入了新的会话管理实现。你可以通过here了解更多。

      【讨论】:

      猜你喜欢
      • 2011-02-27
      • 2018-12-14
      • 1970-01-01
      • 2020-06-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2019-01-19
      相关资源
      最近更新 更多