Minh Nghĩa
Minh Nghĩa

Reputation: 1053

Angular component imported from another Module doesn't render

I'm trying to import a PageComponent, which is declared inside a PageModule. I created them by running ng g m page && cd page && ng g c page, but failed to import PageComponent into the app's app.component.html.

The UI I'm creating will have a fixed sidebar and a <app-page> which will contain the main views. Edit: I'm using ngx-admin and new to Angular. I don't understand their use of <router-outlet>, which is the root component/placeholder for all views, so I'm trying to create my own <app-page>. Hope this helps you to understand my intention and the UI goal.

I followed the checklist in this SO answer:

Angular 2 'component' is not a known element

What can I do to import and use PageComponent?

Update: After restarting the CLI & VSCode several times, the error suddenly disappeared. However, the page still doesn't show the <app-page> component, which includes a <p>page works!</p>.

Upvotes: 3

Views: 3493

Answers (1)

Jonathan002
Jonathan002

Reputation: 10298

Ok so I've created a minimal example of importing a module and being able to use it's component. The example you provided should work, so I'm not sure what the issue could really be without looking at the actual project. (Keep an eye though for imports or extensions that keep local history of the project as you might incorrectly be importing a old module file)

Demo: https://stackblitz.com/edit/angular-mtah9b?file=src%2Fapp%2Fpage%2Fpage.module.ts


As for <router-outlet> it is used for routing in Angular.

Angular's RouterModule will take a config object you provide and setup routing.

// Some RoutingModule

const routesConfig = [
   { path: 'page' component: PageComponent }
];
RouterModule.forRoot(routesConfig) // For Root Module (Usually in AppRouting Module)
RouterModule.forChild(routesConfig) // Child Modules (Usually Lazy Modules)

If the url matches a path you set in your route config. (e.g. https://myapp.com/page)

the PageComponent you set in the routerConfig will be loaded directly below the <router-outlet>

<router-outlet></router-outlet>
<!-- Loaded from url match! -->
<app-page></app-page>

This is an oversimplified answer though and you should read Angular's docs to fully utilize the router or take a look at my personal notes from summary docs

Upvotes: 1

Related Questions