Angular 16组件从共享模块导出不起作用。

huangapple go评论64阅读模式
英文:

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: &#39;app-navbar&#39; is not a known element:
1. If &#39;app-navbar&#39; is an Angular component, then verify that it is part of this module.
2. If &#39;app-navbar&#39; is a Web Component then add &#39;CUSTOM_ELEMENTS_SCHEMA&#39; to the &#39;@NgModule.schemas&#39; of this component to suppress this message.

1 &lt;app-navbar&gt;&lt;/app-navbar&gt;
  ~~~~~~~~~~~~

  src/app/dashboard/dashboard/dashboard.component.ts:5:16
    5   templateUrl: &#39;./dashboard.component.html&#39;,
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component DashboardComponent.

Code for shared.module.ts is below -

import { NgModule } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { NavbarComponent } from &#39;./navbar/navbar.component&#39;;



@NgModule({
  declarations: [
    NavbarComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [
    NavbarComponent
  ]
})
export class SharedModule { }

Code for navbar.component.ts -

import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-navbar&#39;,
  templateUrl: &#39;./navbar.component.html&#39;,
  styleUrls: [&#39;./navbar.component.css&#39;]
})
export class NavbarComponent {

}

Code for dashboard.module.ts -

import { NgModule } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { DashboardComponent } from &#39;./dashboard/dashboard.component&#39;;
import { SharedModule } from &#39;./shared/shared.module&#39;;



@NgModule({
  declarations: [
    DashboardComponent
  ],
  imports: [
    CommonModule,
    SharedModule
  ]
})
export class DashboardModule { }

Code for dashboard.component.ts -

import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-dashboard&#39;,
  templateUrl: &#39;./dashboard.component.html&#39;,
  styleUrls: [&#39;./dashboard.component.css&#39;]
})
export class DashboardComponent {

}

Code for dashboard.component.html -

&lt;app-navbar&gt;&lt;/app-navbar&gt;

Code for app.module.ts -

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { UserAuthModule } from &#39;./user-auth/user-auth.module&#39;;
import { NgbModule } from &#39;@ng-bootstrap/ng-bootstrap&#39;;
import { BrowserAnimationsModule } from &#39;@angular/platform-browser/animations&#39;;
import { PageNotFoundComponent } from &#39;./page-not-found/page-not-found.component&#39;;
import { HttpClientModule } from &#39;@angular/common/http&#39;;

@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 &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { DashboardComponent } from &#39;./dashboard/dashboard.component&#39;;

@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 &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { UserAuthModule } from &#39;./user-auth/user-auth.module&#39;;
import { SharedModule } from &#39;./shared/shared.module&#39;; // 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 转义字符 (&#39;) 替换为了正常的单引号 (') 以使代码更易阅读。

英文:

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 &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;

import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { FeaturedModule } from &#39;./featured/featured.module&#39;;


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FeaturedModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Feature.module -

import { NgModule } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;

import { FeaturedRoutingModule } from &#39;./featured-routing.module&#39;;
import { TestComponent } from &#39;./test/test.component&#39;;
import { SharedModule } from &#39;../shared/shared.module&#39;;


@NgModule({
  declarations: [
    TestComponent
  ],
  imports: [
    CommonModule,
    FeaturedRoutingModule,
    SharedModule,
  ]
})
export class FeaturedModule { }

huangapple
  • 本文由 发表于 2023年7月20日 16:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定