I\'m trying to use a component I created inside the AppModule in other modules. I get the following error though:
\"Uncaught (in promise): Error: Temp
I just had the exact same issue. Before trying some of the solutions posted here, you might want to check if the component really doesn't work. For me, the error was shown in my IDE (WebStorm), but it turned out that the code worked perfectly when i ran it in the browser.
After I shut down the terminal (that was running ng serve) and restarted my IDE, the message stopped showing up.
I had the same problem with Angular CLI: 10.1.5 The code works fine, but the error was shown in the VScode v1.50
Resolved by killing the terminal (ng serve) and restarting VScode.
The problem in my case was missing component declaration in the module, but even after adding the declaration the error persisted. I had stop the server and rebuild the entire project in VS Code for the error to go away.
This convoluted framework is driving me nuts. Given that you defined the custom component in the the template of another component part of the SAME module, then you do not need to use exports in the module (e.g. app.module.ts). You simply need to specify the declaration in the @NgModule directive of the aforementioned module:
// app.module.ts
import { JsonInputComponent } from './json-input/json-input.component';
@NgModule({
declarations: [
AppComponent,
JsonInputComponent
],
...
You do NOT need to import the JsonInputComponent
(in this example) into AppComponent
(in this example) to use the JsonInputComponent
custom component in AppComponent
template. You simply need to prefix the custom component with the module name of which both components have been defined (e.g. app):
<form [formGroup]="reactiveForm">
<app-json-input formControlName="result"></app-json-input>
</form>
Notice app-json-input not json-input!
Demo here: https://github.com/lovefamilychildrenhappiness/AngularCustomComponentValidation
First check: if you have declared- and exported the component inside its module, imported the module where you want to use it and named the component correctly inside the HTML.
Otherwise, you might miss a module inside your routing module:
When you have a routing module with a route that routes to a component from another module, it is important that you import that module within that route module. Otherwise the Angular CLI will show the error: component is not a known element
.
1) Having the following project structure:
├───core
│ └───sidebar
│ sidebar.component.ts
│ sidebar.module.ts
│
└───todos
│ todos-routing.module.ts
│ todos.module.ts
│
└───pages
edit-todo.component.ts
edit-todo.module.ts
2) Inside the todos-routing.module.ts
you have a route to the edit.todo.component.ts
(without importing its module):
{
path: 'edit-todo/:todoId',
component: EditTodoComponent,
},
The route will just work fine! However when importing the sidebar.module.ts
inside the edit-todo.module.ts
you will get an error: app-sidebar is not a known element
.
Fix: Since you have added a route to the edit-todo.component.ts
in step 2, you will have to add the edit-todo.module.ts
as an import, after that the imported sidebar component will work!
In my case, my app had multiple layers of modules, so the module I was trying to import had to be added into the module parent that actually used it pages.module.ts
, instead of app.module.ts
.