简体   繁体   English

Angular 8 基于角色的视图指令

[英]Angular 8 Directive for role based views

I am looking at this example for displaying components based on the role of a user: https://blog.strongbrew.io/display-a-component-based-on-role/我正在查看基于用户角色显示组件的示例: https://blog.strongbrew.io/display-a-component-based-on-role/

My code won't compile because of missing arguments in my constructor inside has-role.directive.spec.ts .由于has-role.directive.spec.ts内的构造函数中缺少 arguments ,我的代码无法编译。 From has-role.directive.ts it takes 3 arguments: ViewContainerRef and TemplateRef from angular core, and a service for getting user roles.has-role.directive.ts它需要 3 个 arguments:来自 angular 核心的 ViewContainerRef 和 TemplateRef,以及用于获取用户角色的服务。

has-role.directive.ts

    constructor(
    private viewContainerRef: ViewContainerRef,
    private templateRef: TemplateRef<any>,
    private authenticationService: AuthenticationService)

But inside the example tutorial its used like this: has-role.directive.spec.ts但是在示例教程中,它的用法是这样的: has-role.directive.spec.ts

 describe('HasRoleDirective', () => {
  it('should create an instance', () => {
    const directive = new HasRoleDirective();
    expect(directive).toBeTruthy();
  });
});

How can this in the example work, but for me it complains about missing arguments?这在示例中如何工作,但对我来说,它抱怨缺少 arguments? 从 Visual Studio


UPDATE:更新:

As Michał suggested i created classes to add to the constructor:正如 Michał 建议的那样,我创建了要添加到构造函数的类:

class TestViewContainerRef extends ViewContainerRef {
    element: ElementRef<any>;
    injector: Injector;
    parentInjector: Injector;
    clear(): void {
        throw new Error('Method not implemented.');
    }
    get(index: number): ViewRef {
        throw new Error('Method not implemented.');
    }
    length: number;
    createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C, index?: number): EmbeddedViewRef<C> {
        throw new Error('Method not implemented.');
    }
    createComponent<C>(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][], ngModule?: NgModuleRef<any>): ComponentRef<C> {
        throw new Error('Method not implemented.');
    }
    insert(viewRef: ViewRef, index?: number): ViewRef {
        throw new Error('Method not implemented.');
    }
    move(viewRef: ViewRef, currentIndex: number): ViewRef {
        throw new Error('Method not implemented.');
    }
    indexOf(viewRef: ViewRef): number {
        throw new Error('Method not implemented.');
    }
    remove(index?: number): void {
        throw new Error('Method not implemented.');
    }
    detach(index?: number): ViewRef {
        throw new Error('Method not implemented.');
    }
}

let viewContainerRefMock = {
  viewContainerRef: TestViewContainerRef
};

describe('HasRoleDirective', () => {
  it('should create an instance', () => {
    const directive = new HasRoleDirective(viewContainerRefMock, templateRefMock, authenticationServiceMock);
    expect(directive).toBeTruthy();
  });
});

Now it complains about my class:现在它抱怨我的 class: 在此处输入图像描述

just create mock classes for constructor parameters, that extend abstractions for your need.只需为构造函数参数创建模拟类,根据您的需要扩展抽象。 for ex.例如。 in has-role.directive.spec.ts createhas-role.directive.spec.ts创建

class TestViewContainerRef extends ViewContainerRef { }
class TestTemplateRef extends TemplateRef<eny> { }
class TestAuthenticationService extends AuthenticationService { }

then implement all needed methods from interfaces.然后从接口实现所有需要的方法。 they can have body like throw new Error("Method not implemented.");他们可以有像throw new Error("Method not implemented.");这样的主体。 - no matter, you will not be using it in such test. - 无论如何,你不会在这样的测试中使用它。 And just create object for each and put into HasRoleDirective constructor.并且只需为每个创建 object 并放入HasRoleDirective构造函数中。

Well, obviously you are missing some arguments as the error message describes.好吧,显然您缺少一些 arguments ,如错误消息所述。 When using the directive in your application, angular creates those 3 dependencies and injects them into your directive.在您的应用程序中使用该指令时,angular 创建这 3 个依赖项并将它们注入您的指令中。

In your example, you are creating an instance of your directive inside a test.在您的示例中,您正在测试中创建指令的实例。 Angular can't inject those dependencies because there is no Angular, it is a test. Angular 无法注入这些依赖项,因为没有 Angular,这是一个测试。 Since Angular is not providing them, you should provide them manually.由于 Angular 没有提供它们,您应该手动提供它们。 This is explained here .这是解释here

The reason the blog you are referring to does not need them is that that blog does not unit test it.您所指的博客不需要它们的原因是该博客没有对其进行单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM