1
votes

ERROR

ERROR Error: Uncaught (in promise): Error: Type PrincipalTemplateComponent is part of the declarations of 2 modules: AppModule and AuthModule! Please consider moving PrincipalTemplateComponent to a higher module that imports AppModule and AuthModule. You can also create a new NgModule that exports and includes PrincipalTemplateComponent then import that NgModule in AppModule and AuthModule. Error: Type PrincipalTemplateComponent is part of the declarations of 2 modules: AppModule and AuthModule! Please consider moving PrincipalTemplateComponent to a higher module that imports AppModule and AuthModule. You can also create a new NgModule that exports and includes PrincipalTemplateComponent then import that NgModule in AppModule and AuthModule

APP MODULE

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HomeComponent } from './components/home/home.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
import { PrincipalTemplateComponent } from './layouts/principal-template/principal-template.component';
import { NavComponent } from './layouts/nav/nav.component';
import { FooterComponent } from './layouts/footer/footer.component';

@NgModule({
  declarations: [
  AppComponent,
  HomeComponent,
  NotFoundComponent,
  PrincipalTemplateComponent,
  NavComponent,
  FooterComponent
 ],
imports: [
  BrowserModule,
  AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

AUTH MODULE

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { SignInComponent } from './sign-in/sign-in.component';
import { LogInComponent } from './log-in/log-in.component';
import { FormComponent } from './form/form.component';
import { PrincipalTemplateComponent } from 'src/app/layouts/principal-template/principal-template.component';
import { FooterComponent } from 'src/app/layouts/footer/footer.component';
import { NavComponent } from 'src/app/layouts/nav/nav.component';

const routes: Routes = [
  { path: 'sign-in', component: SignInComponent },
  { path: 'log-in', component: LogInComponent },
];

@NgModule({
  imports: [CommonModule, RouterModule.forChild(routes)],
  declarations: [
    SignInComponent,
    LogInComponent,
    FormComponent,
    PrincipalTemplateComponent,
    NavComponent,
    FooterComponent
  ]
})
export class AuthModule {}

APP ROUTING MODULE

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
import { NotFoundComponent } from './components/not-found/not-found.component';

const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'auth', loadChildren: './components/auth/auth.module#AuthModule' },
  { path: 'ad', loadChildren: './components/ad/ad.module#AdModule' },
  { path: '**', component: NotFoundComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

HOME COMPONENT && SIGN-IN COMPONENT

<ng-template #template>
  hola
</ng-template>

<app-principal-template></app-principal-template>

<ng-template #template>
  hola 2
</ng-template>

<app-principal-template></app-principal-template>
2
Include the code and the errors as text in the question, not as images.ConnorsFan
ok good.........Ronny Medina
Do I need to create a shared module ?Ronny Medina

2 Answers

0
votes

The whole information you need is in the error message including the solution. Basically you only can 'declare' a component as part of a single module.

For this case, you should create a module that declares this shared component then import this shared 'module' within the other 2 modules, this way you can use the component in these 2 modules.

0
votes

Issue

You had desclared the same component PrincipalTemplateComponent in two modules AppModule and AuthModule.

Fix

You just remove PrincipalTemplateComponent from AppModule

Note : If you are using PrincipalTemplateComponent in other modules (excluding AuthModule) then you should declare in Shared Module.

shared.module.ts

@NgModule({
  declarations: [
    PrincipalTemplateComponent
  ],
  exports:[PrincipalTemplateComponent]
})
export class SharedModule {}