You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 14:03:23