Angular 14集成Bootstrap 5模态框编译报错求助
问题原因
当前安装的@ng-bootstrap/ng-bootstrap版本与Angular 14不兼容。createComponent是Angular 15及以上版本新增的API,高版本ng-bootstrap依赖该API,但Angular 14的@angular/core并未导出这个方法,因此触发编译报错。
解决步骤
- 卸载当前版本的
@ng-bootstrap/ng-bootstrap:npm uninstall @ng-bootstrap/ng-bootstrap - 安装适配Angular 14的ng-bootstrap版本(13.x系列):
npm install @ng-bootstrap/ng-bootstrap@13 - 重新编译项目:
ng serve # 或 ng build
验证
检查package.json中@ng-bootstrap/ng-bootstrap的版本号应为^13.x.x,确认后重启开发服务即可解决报错。
报错信息:
/node_modules/@ng-bootstrap/ng-bootstrap/fesm2020/ng-bootstrap.mjs:17281:25-40 - Error: export 'createComponent' (imported as 'createComponent') was not found in '@angular/core' (possible exports: ANALYZE_FOR_ENTRY_COMPONENTS, ANIMATION_MODULE_TYPE, APP_BOOTSTRAP_LISTENER, APP_ID, APP_INITIALIZER, ApplicationInitStatus, ApplicationModule, ApplicationRef, Attribute, COMPILER_OPTIONS, CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, ChangeDetectorRef, Compiler, CompilerFactory, Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, ContentChild, ContentChildren, DEFAULT_CURRENCY_CODE, DebugElement, DebugEventListener, DebugNode, DefaultIterableDiffer, Directive, ENVIRONMENT_INITIALIZER, ElementRef, EmbeddedViewRef, EnvironmentInjector, ErrorHandler, EventEmitter, Host, HostBinding, HostListener, INJECTOR, Inject, InjectFlags, Injectable, InjectionToken, Injector, Input, IterableDiffers, KeyValueDiffers, LOCALE_ID, MissingTranslationStrategy, ModuleWithComponentFactories, NO_ERRORS_SCHEMA, NgModule,...)
内容的提问来源于stack exchange,提问作者athi

