Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Angular 4 AOT compiler does not support mixins

Sometimes I use Mixins to inject repeated functions like slugUrl().

But it doesn't work with angular 4 compiler.

export function Mixin(decorators: Function[]) {
  return function (classFn: Function) {
    decorators.forEach(decorator => {
      Object.getOwnPropertyNames(decorator.prototype).forEach(name => {
        classFn.prototype[name] = decorator.prototype[name];
      });
    });
  };
}


@Mixin([BehaviorInjected])
export class FooComponent {

}

If I compile this code, compiler throws:

Property 'ngClassControl' does not exist on type 'FooComponent'.

Any ideas?

Edit: Since there was someone who asked, here's another example using TS mixins that reproduces the issue, this time at template level.

Components:

@Component({
    selector: 'home-page',
    template: '<test [tag]="tag"></test>'
})
export class HomePageComponent extends TaggedComponent(MyComponent) {
    public tag = 'hi there';
}

@Component({
    selector: 'test',
    template: '<div></div>'
})
export class TestComponent extends TaggedComponent(MyComponent) {}

Mixins:

type Constructor<T> = new(...args: any[]) => T;

export function TaggedComponent<T extends Constructor<{}>>(Base: T) {
     class TaggedBase extends Base {
        @Input() tag: string;
     };

     return TaggedBase;
}

export class MyComponent {
    protected subscriptions: Subscription = new Subscription();
  // ...
}

Error:

ERROR in Error: Template parse errors: Can't bind to 'tag' since it isn't a known property of 'test'. ("][tag]="tag">")

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The main problem here is that angular compiler has limited functionality.(read more in the docs)

AOT compiler uses metadata that is generated by MetadataCollector. It uses typescript object model(tree of Node)(that's why AOT can be used only with typescript) to gather all information that is necessary to produce ngfactory (in some cases also ngsummary) files.

The examples you provided are completely different for AOT compiler:

1) Custom decorator

@Mixin([BehaviorInjected])
export class FooComponent {}

Angular MetadataCollector will include @Mixin decorator in metadata of FooComponent symbol(item in decorators array) but it will be skipped when aot StaticReflector will call simplify since Mixin decorator is not registered in special map that includes only strictly defined decorators (source code)

Moreover if we even include it in that map it still won't be executed during aot compilation because it is only available for supported decorators.

2) Call custom function

export class HomePageComponent extends TaggedComponent(MyComponent) {

MetadataCollector will add TaggedComponent to metadata collection as symbol like {__symbolic: 'error', message: 'Symbol reference expected'}; but it also will be skipped inside StaticReflector.

And as far as i know there is currently no solution to support it.

over 4 years ago · Santiago Trujillo Relatório

0

See https://github.com/angular/angular/issues/19145

I believe it is the same problem. Decorator inheritance is broken for mixins, so currently you will have to duplicate decorated properties.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda