Using modules and their library dependencies at angular 9

Asked

Viewed 134 times

0

I’m creating a library with angular 9 to be used in several projects and would like to know if it is possible to use dependencies within a module without having to import them in the main application (the one that uses the library):

For example: I created the module MaterialModule inside my library

import { MatBadgeModule } from '@angular/material/badge';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatListModule } from '@angular/material/list';
import { MatMenuModule } from '@angular/material/menu';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatTableModule } from '@angular/material/table';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { NgModule } from '@angular/core';

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatSidenavModule,
    MatListModule,
    MatButtonModule,
    MatBadgeModule,
    MatCardModule,
    MatProgressSpinnerModule,
    MatMenuModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatBadgeModule,
    MatSidenavModule,
    MatInputModule,
    MatMenuModule,
    MatListModule,
    MatProgressSpinnerModule,
    MatTooltipModule,
    MatGridListModule,
    MatCardModule,
    MatTableModule,
    MatButtonModule
  ]
})
export class MaterialModule {}

In the module of the main application, would import MaterialModule in screen1.modules.ts:

import { NgModule } from '@angular/core';
import { Screen1Component } from './screen1.component';
import { MaterialModule } from 'componentes-web/lib/shared/material-module';

@NgModule({
  imports: [MaterialModule],
  declarations: [Screen1Component],
  exports: [Screen1Component]
})
export class Screen1Module {}

Using the tag

<mat-card>
of the angular material in html screen1.component.html:

<mat-card class="example-card">
  <mat-card-header>
    <mat-card-title>{{'titulo.card' | translate}}</mat-card-title>
    <mat-card-subtitle>{{'sub-titulo.card' | translate}}</mat-card-subtitle>
  </mat-card-header>
  <mat-card-content>

  </mat-card-content>
  <mat-card-actions>
    <button mat-raised-button color="primary">{{'btn.novo' | translate}}</button>
  </mat-card-actions>
</mat-card>

That is, use the tag

<mat-card>
without the need to import MatCardModule in my module Screen1Module:

import { NgModule } from '@angular/core';
import { Screen1Component } from './screen1.component';
import { MatCardModule } from '@angular/material/card';

@NgModule({
  imports: [MatCardModule],
  declarations: [Screen1Component],
  exports: [Screen1Component]
})
export class Screen1Module {}

In my library, I’m exporting the module MaterialModule:

export * from './lib/componentes-web.component';
export * from './lib/componentes-web.module';
export * from './lib/componentes-web.service';

//material modulo
export * from './lib/shared/material-module';

But my file material-module.d.ts has only:

export declare class MaterialModule {
}

  • It’s not that there’s no way, but it seems to cause more problems than solve, as I see it, makes more having a template of this file and paste it into your projects. So you get the ease, but keep the versatility of each project importing its modules

  • what trouble you’re in?

  • Says that MaterialModule has not @NgModule with the Imports.... in general, I wanted to use in my main projects, the modules of the material angular only importing MaterialModule and this would give me access to the modules of the angular material type by extension

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.