The Element is Unfamiliar - Application with Multiple Modules

I seem to be facing an issue with how my modules are structured, as I am unable to use shared components across different modules.

Basically, I have a Core module and a Feature module. The Core module contains components that I want to share across multiple Feature modules. The Feature module imports the Core module.

Within the FeatureModule, I have a component named PageSectionComponent that I want to display on a dashboard page. However, when trying to use the selector, I receive the error

page-section is not a known element
.

FilePath

src
   - components
      - site-layout.component.html
      - site-layout.component.ts
   - modules
      - core
         - core-routing.module.ts
         - core.module.ts
      - feature
         - pages
            - dashboard-page.component.html
            - dashboard-page.component.ts
         - components
            - page-section.component.html
            - page-section.component.ts
         - feature-routing.module.ts
         - feature.module.ts

feature.module.ts

import { NgModule } from '@angular/core';

import { FeatureRoutingModule } from './feature-routing.module';
import { CoreModule } from '../core/core.module';

@NgModule({
  imports: [
    CoreModule,
    FeatureRoutingModule
  ]
})
export class FeatureModule {}

core.module.ts - imports both the SiteLayout and the PageSection.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SiteLayoutComponent } from 'app/components';
import { CoreRoutingModule } from './core-routing.module';

import { PageSectionComponent } from '@feature/components';

@NgModule({
  declarations: [
    PageSectionComponent,
    SiteLayoutComponent
  ],
  imports: [
    CommonModule,
    CoreRoutingModule,
  ],
  exports: [
    PageSectionComponent,
    SiteLayoutComponent
  ]
})
export class CoreModule {}

The problem seems to lie within my routing configuration. I am using the SiteLayoutComponent to wrap pages from both the Core and Feature modules. Thus, my FeatureRoutingComponent looks like this, where all paths include the SiteLayoutComponent, followed by the specific Dashboard or other component based on the path.

feature-routing.component.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SiteLayoutComponent } from '@components/layouts/site-layout.component';
import { DashboardPageComponent } from '@feature/pages';

const routes: Routes = [
  {
    path: '',
    component: SiteLayoutComponent,
    children: [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, 
      { path: 'dashboard',  component: DashboardPageComponent }
    ]
  }
];

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

Currently, the SiteLayoutComponent only contains the router-outlet.

site-layout.component.html

<router-outlet></router-outlet>

My trouble starts when attempting to place a 'PageSectionComponent' in the DashboardPageComponent.

dashboard-page.component.html

<page-section></page-section>

This results in the error stating that page-section is not a recognized element.

After researching multi-module applications, it seems that adding the PageSectionComponent to the CoreModule's imports + exports and then importing the CoreModule within the FeatureModule should work, but it doesn't.

My current hypothesis is that because the Site-Layout is a shareable component referenced directly in feature-routing.module.ts AND core.module.ts, it may be causing the imported PageSectionComponent to lose scope somehow.

Answer №1

Hey there, I see that you're having trouble with declaring your DashboardPageComponent in the code snippet provided. It seems like defining this component is necessary before proceeding further.

To resolve this issue, consider incorporating a declaration similar to the following:

import { NgModule } from '@angular/core';

import { FeatureRoutingModule } from './feature-routing.module';
import { DashboardPageComponent } from './feature.module';
import { CoreModule } from '../core/core.module';

@NgModule({
  declarations:[
     DashboardPageComponent 
  ],
  imports: [
    CoreModule,
    FeatureRoutingModule
  ],
  exports: [
    DashboardPageComponent
   ]
})
export class FeatureModule {}

Remember to ensure all components are imported properly within your project structure.

Moreover, revisiting the modularity approach might be beneficial as the current setup appears to be somewhat confusing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upon updating my application from Angular 14 to 16, I encountered an overwhelming number of errors within the npm packages I had incorporated

After upgrading my angular application from v14 to v16, I encountered numerous peer dependencies issues, which led me to use the --force flag for the upgrade process. However, upon compiling, I am now faced with a multitude of errors as depicted in the scr ...

When converting to a React Functional Component using Typescript, an error occurred: The property 'forceUpdateHandler' could not be found on the type 'MutableRefObject<Spinner | null>'

Looking to convert the App component in this CodePen into a Functional component using Typescript. Encountering an error when attempting to run it: ERROR in src/App.tsx:13:14 TS2339: Property 'forceUpdateHandler' does not exist on type 'Mu ...

Is it possible to use the Optimistic Hook with boolean values?

I am facing an issue with a switch component where the checked value is updated only after refetching the data when the user is changed to an admin status. Currently, there is a delay when clicking the switch as it takes time to load and then updates. It ...

Tips for infuriating TSC with Lookup categories

I'm looking for the Typescript compiler (TSC) to throw errors when I make mistakes in signatures. export class EventEmitter<EventTypes extends { [key: string]: any }> { subscribe<Event extends keyof EventTypes>(type: keyof EventTypes, ...

React component's state is not being correctly refreshed on key events

Currently facing an issue that's puzzling me. While creating a Wordle replica, I've noticed that the state updates correctly on some occasions but not on others. I'm struggling to pinpoint the exact reason behind this discrepancy. Included ...

The inserted button's click event does not trigger the contentEditable DIV

I have a contentEditable DIV that I manage using the directive below: <div class="msg-input-area" [class.focused]="isMsgAreaFocused" contenteditable [contenteditableModel]="msgText" (contenteditableModelChang ...

Conditionals in Angular 2 using Sass

Looking to apply this style with Sass or CSS: IF :host.form-control MATCHES .ng-valid[required] OR .ng-valid.required THEN :host ::ng-deep input.form-control = border-left: 5px solid #42A948; Appreciate the help! ...

The CanDeactivate feature appears to be malfunctioning

I'm currently working on detecting router deactivation. Below is the definition of the router: export const AppRoutes: Routes = [ { path: '', component: HelloComponent, canDeactivate: [ConfirmDeactivateGuard] }, { pat ...

Using Angular 2 to bind ngModel to a property's reference

I have a lengthy list of inputs provided by users that I would like to store in an object instead of listing them out in HTML. My goal is to connect these values to another object that holds the data of the user or customer. I am looking to use ngModel for ...

Navigating through various product categories in Angular's routing system

Greetings! I am currently building a Shop Page in Angular 4 and encountering an obstacle with Angular routing. The issue arises when a user clicks on a product category, the intention is for the website to direct them to the shop page. On the homepage, th ...

Return the subclass from the constructor function

class X{ constructor(input: string) { // do things } f() {console.log("X")} } class Y extends X{ constructor(input: string) { // do things } f() {console.log("Y")} } class Z extends X{ con ...

My component is displaying a warning message that advises to provide a unique "key" prop for each child in a list during rendering

I need help resolving a warning in my react app: Warning: Each child in a list should have a unique "key" prop. Check the render method of `SettingRadioButtonGroup`. See https://reactjs.org/link/warning-keys for more information. at div ...

Is there a way to send routerLink to an HTML element like <div [innerHTML]=""> without triggering the warning: "sanitizing HTML stripped some content"? Check out https://g.co/ng/security#xss for more information

Within the parent component, I am using the following construction: const link = `<a routerLink="${group.id}">${group.name}</a>`; // also tried using [routerLink] When attempting to work with it in a child component, I implement it l ...

Troubleshooting Angular 2 Component: Why is console.log not functioning in Typescript?

I'm fairly new to both Angular2 and typescript. Given that typescript is a superset of javascript, I assumed that functions like console.log would function as expected. Interestingly, console.log works perfectly in .ts files when placed outside a comp ...

Trigger a function upon change of selected value in Ionic 3

Here is some HTML code to consider: <ion-select interface="popover" [(ngModel)]="selectV"> <ion-option *ngFor="let human of humans" [value]="v.id">{{v.name}}</ion-option> <ion-option onChange="openModal()">GO TO THE ...

What is the reason behind permitting void functions in the left part of an assignment in Typescript?

Take a look at this Typescript snippet: let action = function (): void { //perform actions }; let result = action(); What makes it suitable for the TypeScript compiler? ...

Error message in Angular when promises are not defined

Recently, I started working with promises for the first time. I have a function that returns a promise: public DatesGenerator(futercampaign: ICampaign, searchparam: any, i: number): ng.IPromise<any> { return this.$q((resolve, reject) => { ...

Setting up Angular 2 for ASP.NET MVC: A Step-by-Step Guide

angular.io provides a setup guide for asp.net core at: https://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html. However, I am trying to configure it for an asp.net mvc application. The Quick Start files are already present in the asp.net mvc te ...

Issue with Angular4: Unable to select [selected] attribute on initial load

Here is a code snippet to select a department: <select name="department" class="form-control select" [(ngModel)]="departments" formControlName="departmentControl"> <option *ngFor="let department of departments" [ngValue]="department" [se ...

Universal in Angular is malfunctioning

As a newcomer to Angular 4, I am looking to create an Angular app that is SEO friendly and supports Angular Universal (with the --universal flag after ung new or ung init). I have successfully created an Angular Universal app. However, when running the p ...