英文:
Angular 16 component export from a shared module not working
问题
我已经为我的Angular 16项目创建了一个共享模块并导出它。问题是,当我将其导入到app.module.ts时,它按预期工作。但是当我将其导入到dashboard.module.ts时,它出现以下错误 -
Error: src/app/dashboard/dashboard/dashboard.component.html:1:1 - error NG8001: 'app-navbar' is not a known element:
1. If 'app-navbar' is an Angular component, then verify that it is part of this module.
2. If 'app-navbar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
1 <app-navbar></app-navbar>
~~~~~~~~~~~~
src/app/dashboard/dashboard/dashboard.component.ts:5:16
5 templateUrl: './dashboard.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component DashboardComponent.
shared.module.ts的代码如下 -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NavbarComponent } from './navbar/navbar.component';
@NgModule({
declarations: [
NavbarComponent
],
imports: [
CommonModule
],
exports: [
NavbarComponent
]
})
export class SharedModule { }
navbar.component.ts的代码如下 -
import { Component } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent {
}
dashboard.module.ts的代码如下 -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardComponent } from './dashboard/dashboard.component';
import { SharedModule } from './shared/shared.module';
@NgModule({
declarations: [
DashboardComponent
],
imports: [
CommonModule,
SharedModule
]
})
export class DashboardModule { }
dashboard.component.ts的代码如下 -
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent {
}
dashboard.component.html的代码如下 -
<app-navbar></app-navbar>
app.module.ts的代码如下 -
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserAuthModule } from './user-auth/user-auth.module';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
AppRoutingModule,
UserAuthModule,
NgbModule,
BrowserAnimationsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我尝试以与app.module.ts中相同的方式导入Navbar组件并在app.component.html中使用它,然后它可以正常工作。但在Dashboard模块中会出错。
英文:
I have created a shared module for my project in Angular 16 and exporting it. Problem is when I am importing this to app.module.ts, it's working as expected. But when I import this to dashboard.module.ts, it's giving following error -
Error: src/app/dashboard/dashboard/dashboard.component.html:1:1 - error NG8001: 'app-navbar' is not a known element:
1. If 'app-navbar' is an Angular component, then verify that it is part of this module.
2. If 'app-navbar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
1 <app-navbar></app-navbar>
~~~~~~~~~~~~
src/app/dashboard/dashboard/dashboard.component.ts:5:16
5 templateUrl: './dashboard.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component DashboardComponent.
Code for shared.module.ts is below -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NavbarComponent } from './navbar/navbar.component';
@NgModule({
declarations: [
NavbarComponent
],
imports: [
CommonModule
],
exports: [
NavbarComponent
]
})
export class SharedModule { }
Code for navbar.component.ts -
import { Component } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent {
}
Code for dashboard.module.ts -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardComponent } from './dashboard/dashboard.component';
import { SharedModule } from './shared/shared.module';
@NgModule({
declarations: [
DashboardComponent
],
imports: [
CommonModule,
SharedModule
]
})
export class DashboardModule { }
Code for dashboard.component.ts -
import { Component } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent {
}
Code for dashboard.component.html -
<app-navbar></app-navbar>
Code for app.module.ts -
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserAuthModule } from './user-auth/user-auth.module';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
AppRoutingModule,
UserAuthModule,
NgbModule,
BrowserAnimationsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
I tried to import Navbar component in exactly same way to app.module.ts and used in app.component.html,then it works fine. But it gives error in Dashboard Module.
答案1
得分: 1
为了解决这个问题,您需要确保 SharedModule
只在模块层次结构中的较高级别处导入一次,比如 AppModule
。以下是如何修复的方式:
从 DashboardModule
中移除对 SharedModule
的导入,因为它已经在 AppModule
中导入了。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardComponent } from './dashboard/dashboard.component';
@NgModule({
declarations: [DashboardComponent],
imports: [CommonModule],
})
export class DashboardModule {}
确保在 AppModule
中导入 SharedModule
,这样它就可以对应用中的所有其他模块可用。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserAuthModule } from './user-auth/user-auth.module';
import { SharedModule } from './shared/shared.module'; // 在这里导入 SharedModule
// ... 其他导入 ...
@NgModule({
declarations: [AppComponent, PageNotFoundComponent],
imports: [
BrowserModule,
AppRoutingModule,
UserAuthModule,
NgbModule,
BrowserAnimationsModule,
HttpClientModule,
SharedModule, // 在这里添加 SharedModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
通过这个配置,SharedModule
将被导入到 AppModule
中,并且它的组件,包括 NavbarComponent
,将对所有其他特性模块可用。
英文:
To resolve this, you need to make sure that the SharedModule
is only imported once and at a higher level in the module hierarchy, such as the AppModule
. Here's how you can fix it:
Remove the SharedModule
import from the DashboardModule
since it's already being imported in the AppModule
.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardComponent } from './dashboard/dashboard.component';
@NgModule({
declarations: [DashboardComponent],
imports: [CommonModule],
})
export class DashboardModule {}
Make sure to import SharedModule
in the AppModule
so that it's available to all other modules in the application.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserAuthModule } from './user-auth/user-auth.module';
import { SharedModule } from './shared/shared.module'; // Import SharedModule here
// ... other imports ...
@NgModule({
declarations: [AppComponent, PageNotFoundComponent],
imports: [
BrowserModule,
AppRoutingModule,
UserAuthModule,
NgbModule,
BrowserAnimationsModule,
HttpClientModule,
SharedModule, // Add SharedModule here
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
With this configuration, the SharedModule
will be imported in the AppModule
, and its components, including the NavbarComponent
, will be available to all other feature modules, including the DashboardModule
.
答案2
得分: 1
我找到了一个解决方案,看起来它正在运行。请问这是否是正确的方法?
我将特性模块导入到了应用程序模块,而不是共享模块。之后,我将共享模块导入到特性模块中。现在一切都正常了。
App.module -
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FeaturedModule } from './featured/featured.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FeaturedModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Feature.module -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeaturedRoutingModule } from './featured-routing.module';
import { TestComponent } from './test/test.component';
import { SharedModule } from '../shared/shared.module';
@NgModule({
declarations: [
TestComponent
],
imports: [
CommonModule,
FeaturedRoutingModule,
SharedModule,
]
})
export class FeaturedModule { }
请注意,我已经将代码中的 HTML 转义字符 ('
) 替换为了正常的单引号 ('
) 以使代码更易阅读。
英文:
I found a solution and seems it's working. Kindly suggest if this is right approach.
I imported feature module instead of shared module to App module. After that I imported shared module to feature module. And it's working fine now.
App.module -
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FeaturedModule } from './featured/featured.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FeaturedModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Feature.module -
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeaturedRoutingModule } from './featured-routing.module';
import { TestComponent } from './test/test.component';
import { SharedModule } from '../shared/shared.module';
@NgModule({
declarations: [
TestComponent
],
imports: [
CommonModule,
FeaturedRoutingModule,
SharedModule,
]
})
export class FeaturedModule { }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论