Angular 17独立应用集成Nebular出现R3InjectorError求助
独立组件模式下解决Nebular v13的NullInjectorError问题
1. 根入口配置Nebular核心服务
打开main.ts,将Nebular主题模块的初始化providers注入到bootstrap配置中——因为独立组件模式没有app.module.ts,核心服务需要在这里统一提供:
import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app/app.component'; import { provideAnimations } from '@angular/platform-browser/animations'; import { NbThemeModule } from '@nebular/theme'; bootstrapApplication(AppComponent, { providers: [ provideAnimations(), // Nebular依赖Angular动画,必须添加 ...NbThemeModule.forRoot({ name: 'default' }).providers, // 注入Nebular核心服务集群 ], });
2. 组件内导入按钮模块
在app.component.ts中,把NbButtonModule加入组件的imports数组,nbButton指令属于这个模块,必须导入才能使用:
import { Component } from '@angular/core'; import { NbButtonModule } from '@nebular/theme'; @Component({ selector: 'app-root', standalone: true, imports: [NbButtonModule], templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { title = 'nebular-standalone-demo'; }
3. 验证依赖完整性
如果仍有报错,先确认已安装所有必需依赖:
npm install @nebular/theme @angular/cdk @angular/animations
若仍出现特定服务缺失,可直接将对应服务添加到组件的providers数组,但优先使用NbThemeModule.forRoot()的方式,它会自动处理绝大多数核心服务的依赖关系。
内容的提问来源于stack exchange,提问作者M.Mac
相关产品推荐
相关产品推荐

