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

196
Visualizações
How can I export lodash from my Angular module?

I would like to make a generic module that has a bunch of utility methods, including moment and lodash functionality. I've imported lodash and typed it and I can access it from my components as long as I include the code:

import * as _ from "lodash";

However, I'd like to make this more generic so I can just import my Utils module and have both lodash and moment (as well as a few custom functions) available without having to import these individually in every component.

I've got the following code:

import { Injectable, NgModule } from "@angular/core";
import * as _ from "lodash";
import * as _ from "moment";

@Injectable()
@NgModule({
    exports: [_, moment]
})
export class Utils { }
}

I get the following error:

Build:Argument of type '{ exports: LoDashStatic[] 1>; }' 
   is not assignable to parameter of type 'NgModule'

Edit: What I'd like to do is this:

import { _, moment } from "./Utils.module";

instead of

import * as _ from "lodash";
import * as moment from "moment";

How can I accomplish this?

Edit: Made the code reflect more of what I'd like to accomplish...

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

0

This

@NgModule({
    exports: [_, moment]
})
...

won't work (and thanks to TypeScript, it can detect the issue with type checking), because exports contains for Angular modules, while these are ES6 modules:

import { _, moment } from "./Utils.module";

Instead, it may be not .module.ts file, which are conventionally used for Angular modules, but utils.ts:

import * as _ from "lodash";
import * as moment from "moment";
export { _, moment };

And usually the one should never want to have a separate 'util' file to import third-party packages from it like import { _, moment } from "./utils". The path is relative and needs to be maintained in each file where it is imported. The paths to the packages are not, that's their major advantage.

The whole thing looks like modular antipattern. You won't see this in well-designed projects, because it makes little sense. I would personally recommend to not do this, unless you are really sure what are your benefits here.

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