I cannot import a feature module into the main module. Angular

Asked

Viewed 444 times

2

I have a functionality module called CategoriasModule and when I put the class name in the main module’s Imports this error appears:

Uncaught Error: Unexpected value 'Undefined' Imported by the module 'Appmodule'.

When I take the CategoriasModule of Imports of AppModule, everything works ok. Is there any way I can use the module regardless in the main module? If not, how do I solve this problem?

app.modulets.:

...
import { CategoriasModule } from './categorias/categorias.module';
...
@NgModule({
  declarations: [
    AppComponent,
    TopoComponent,
    HomeComponent,
    RodapeComponent,
    RestaurantesComponent,
    DiversaoComponent,
    OfertaComponent,
    ComoUsarComponent,
    OndeFicaComponent,
    DescricaoReduzida,
    OrdemCompraComponent,
    InfantilComponent,
    FitnessComponent,
    TeenComponent,
    CalcadosComponent,
    //CategoriasComponent
    BannerSlideComponent,
    OrdemCompraSucessoComponent,
    //ajustanto o erro ngIf

    //
  ],
  imports: [
    CategoriasModule,
    BrowserModule,
    HttpModule,
    //ajustanto o erro ngIf

    ReactiveFormsModule,

    //
    RouterModule.forRoot(ROUTES)
  ],
  providers: [ 
    CarrinhoService, { provide: LOCALE_ID, useValue: 'pt-Br' } 
  ],
  bootstrap: [AppComponent],
  //
  schemas: [ CUSTOM_ELEMENTS_SCHEMA,
    NO_ERRORS_SCHEMA
  ]
  //
})
export class AppModule { }

categories.modulets.:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CategoriasComponent } from './categorias.component';
import { Categoria01Component } from './categoria01/categoria01.component';

@NgModule({
    declarations: [
        CategoriasComponent,
        Categoria01Component
    ],
    imports: [ CommonModule ],
    exports: [],
    providers: [],
})
export class CategoriasModule {}
  • Friend, because you want to put a module, inside the main module?

2 answers

0

I believe the problem is happening because you are not exporting the components.

Solution

Appmodule.ts

...
import { CategoriasModule } from './categorias/categorias.module';
...
@NgModule({
  declarations: [
    AppComponent,
    TopoComponent,
    HomeComponent,
    RodapeComponent,
    RestaurantesComponent,
    DiversaoComponent,
    OfertaComponent,
    ComoUsarComponent,
    OndeFicaComponent,
    DescricaoReduzida,
    OrdemCompraComponent,
    InfantilComponent,
    FitnessComponent,
    TeenComponent,
    CalcadosComponent,
    //CategoriasComponent
    BannerSlideComponent,
    OrdemCompraSucessoComponent,
    //ajustanto o erro ngIf

    //
  ],
  imports: [
    CategoriasModule,
    BrowserModule,
    HttpModule,
    //ajustanto o erro ngIf

    ReactiveFormsModule,

    //
    RouterModule.forRoot(ROUTES)
  ],
  providers: [ 
    CarrinhoService, { provide: LOCALE_ID, useValue: 'pt-Br' } 
  ],
  bootstrap: [AppComponent],
  //
  schemas: [ CUSTOM_ELEMENTS_SCHEMA,
    NO_ERRORS_SCHEMA
  ]
  //
})
export class AppModule { }

categories.modulets.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CategoriasComponent } from './categorias.component';
import { Categoria01Component } from './categoria01/categoria01.component';

@NgModule({
    declarations: [
        CategoriasComponent,
        Categoria01Component
    ],
    imports: [ CommonModule ],
    exports: [             // Modificado
        CategoriasComponent, 
        Categoria01Component
    ],
    providers: [],
})
export class CategoriasModule {}

Try this and see if it works. I hope I helped.

0

A Component can only be in the Declarations section of 1 Module, and the Categoriascomponent component is correctly in the Categoriasmodule module.

Now for you to use this component you have its forms or put it in Appmodule inside Imports and Exports also (to export to other components) or else Voce can only import this module in the module Imports of the component Voce will use, what would be more indicated, in this case does not need the Exports, and the Categoriascomponent, Oce nor need to import in any module only in the Component where Voce will use, since the Categoriasmodulo will already be in the import of the module of the component in question.

Browser other questions tagged

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