在angular 6中使用ng-bootstrap modal时出现问题。
首先,我有2个模块:
AppModule
和ComponentModule
这里是ComponentModule
:
@NgModule({
imports: [
CommonModule,
FormsModule,
ComponentRoutingModule,
BsDropdownModule.forRoot(),
TabsModule,
PaginationModule.forRoot(),
PopoverModule.forRoot(),
ProgressbarModule.forRoot(),
TooltipModule.forRoot(),
ModalModule.forRoot(),
NgMultiSelectDropDownModule.forRoot(),
],
declarations: [
CategoryComponent,
CourseComponent,
ModalComponent,
CategoryModalComponent,
],
entryComponents: [CategoryModalComponent],
})
export class ComponentModule { }
ComponentModule
是我放置要在登录后显示的组件的地方。
在ComponentModule
中,我有一个课程组件:
@Component({
selector: 'app-course',
template: '<app-modal></app-modal>',
styleUrls: ['./course.component.scss']
})
export class CourseComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
以下是同一模块中的ModalComponent
:
@Component({
selector: 'app-modal',
template: '<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>',
styleUrls: ['./modal.component.scss']
})
export class ModalComponent implements OnInit {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(CategoryModalComponent);
modalRef.componentInstance.name = 'World';
}
ngOnInit() {
}
}
和
@Component({
selector: 'app-category-modal',
template: `
<div class="modal-header">
<h4 class="modal-title">Hi there!</h4>
<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Hello, {{name}}!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
</div>
`,
styleUrls: ['./category-modal.component.scss']
})
export class CategoryModalComponent implements OnInit {
@Input() name;
constructor(public activeModal: NgbActiveModal) {}
ngOnInit() {
}
}
我希望当我单击ModalComponent
中的按钮时,将显示一个模式,表明其内容位于CategoryModalComponent
中。但这给我显示了一个错误:
ModalComponent.html:4 ERROR Error: No component factory found for CategoryModalComponent. Did you add it to @NgModule.entryComponents?
at noComponentFactoryError (core.js:3256)
at CodegenComponentFactoryResolver.push../node_modules/@angular/core/fesm5/core.js.CodegenComponentFactoryResolver.resolveComponentFactory (core.js:3291)
at NgbModalStack.push../node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js.NgbModalStack._createFromComponent (ng-bootstrap.js:7643)
at NgbModalStack.push../node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js.NgbModalStack._getContentRef (ng-bootstrap.js:7581)
at NgbModalStack.push../node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js.NgbModalStack.open (ng-bootstrap.js:7444)
根据错误,我在ComponentModule
中添加了该错误,但是它不起作用。
请问您有什么建议可以解决吗?
答案 0 :(得分:1)
将NgbModalModule或NgbModule添加到您的ComponentModule。
“组件”模块中没有ngb模块。
nb:在您的情况下进行了尝试和工作。
import { NgbModalModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
imports: [
...
NgbModalModule
],
...
})
export class ComponentModule { }
答案 1 :(得分:0)
您应该在模块中添加“ CategoryModalComponent”作为声明和entryComponent。