简体   繁体   English

ng-bootstrap和Angular 6:无法解析NgbModalRef的所有参数:(?,?,?,?)

[英]ng-bootstrap and Angular 6: Can't resolve all parameters for NgbModalRef: (?,?,?,?)

Has anyone used ng-bootstrap and come across the following error with angular 6 有没有人使用过ng-bootstrap并遇到角度为6的以下错误

控制台窗口

I have included 我已经包括

imports:[NgbModule]
providers: [NgbModule, NgbModalRef]

in the app.module.ts file 在app.module.ts文件中

inside my ts component file i have imported 在我导入的ts组件文件中

import { NgbModal, NgbModalRef } from "@ng-bootstrap/ng-bootstrap";

 constructor(private formBuilder: FormBuilder,
        private modalService: NgbModal,
        private activeModel: NgbModalRef,
        private readonly resourceService: ResourceService,
        private readonly authService: AuthService,
        private readonly userService: UserService,
        private readonly competitionService: CompetitionService){}

open(content: any, modal?: ICompetition) {
        if (modal == undefined) {
            this.competitionFrm.setValue({
                competitionKey: null,
                centre: this.user.Centre,
                programme: "",
                competitionName: ""
            });
            this.activeModel = this.modalService.open(content,{ ariaLabelledBy: "modal-basic-title", backdrop: "static", keyboard: false });
            this.activeModel.result.then((result) => {
                    this.closeResult = `Closed with: ${result}`;
                },
                    (reason) => {
                        this.closeResult = `Dismissed with: ${reason}`;
                    });
        } else {
            this.competitionFrm.setValue({
                competitionKey: modal.competitionKey,
                centre: modal.centre,
                programme: modal.programme,
                competitionName: modal.competitionName
            });
        }
    }

saveDetails(model: any): void {
        this.competitionService.save(model).subscribe(
            data => {
                if (data.Successful === false) {
                    this.modelMsg = data.Information;
                } else {
                    this.msg = data.Information;
                    this.getCompetitions();
                    this.activeModel.close();
                }

            },
            error => this.msg = <any>error);
    }

does anyone have any knowledge as to the error that is being displayed in the browsers console window. 有没有人对浏览器控制台窗口中显示的错误有任何了解。 I have seen some other issues from a while ago saying this was solved back in 2017 however the issue still seems to be there. 我不久前还看到了其他一些问题,这些问题说早在2017年就已解决,但是问题似乎仍然存在。

The purpose for this is to close the open modal from inside the save function. 这样做的目的是从保存功能内部关闭打开模式。

Many thanks in advance 提前谢谢了

Lewis 刘易斯

UPDATE: app.module.ts 更新:app.module.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { CommonModule, APP_BASE_HREF } from "@angular/common";
import { MatIconModule, MatFormFieldModule, MatSelectModule, MatDialogModule, } from "@angular/material";
import { ReactiveFormsModule, FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgbModule, NgbActiveModal, NgbModalRef} from '@ng-bootstrap/ng-bootstrap';


import { AppComponent } from "./app.component";
import { FooterComponent } from "./components/footer/footer.component";
import { HeaderComponent } from "./components/header/header.component";
import { NavBarComponent } from "./components/NavBar/navbar.component";
import { HomeComponent } from "./components/home/home.component";
import { AccountManagementComponent } from "./components/accountManagement/accountManagement.component";
import { CompetitionManagementComponent } from "./components/competitionManagement/competitionManagement.component";

import { appRoutingModule } from "./app.routing";
import { UserService } from "./Service/user.service";
import { LoginService } from "./Service/login.service";
import { AuthService } from "./Service/auth.service";
import { ResourceService } from "./Service/resource.service";
import { CompetitionService } from "./Service/competition.service";


@NgModule({
  declarations: [
      AppComponent,
      FooterComponent,
      HeaderComponent,
      NavBarComponent,
      HomeComponent,
      AccountManagementComponent,
      CompetitionManagementComponent
  ],
    imports: [
      NgbModule.forRoot(),
      BrowserModule,
      appRoutingModule,
      MatIconModule,
      MatFormFieldModule,
      MatSelectModule,
      MatDialogModule,
      ReactiveFormsModule,
      FormsModule,
      CommonModule,
      BrowserAnimationsModule,
      HttpClientModule
  ],
    providers: [{ provide: APP_BASE_HREF, useValue: '/' }, UserService, LoginService, AuthService, ResourceService, CompetitionService, NgbModule, NgbModalRef],
    bootstrap: [AppComponent],
    entryComponents: [AccountManagementComponent]
})
export class AppModule { }

So the solution to this is not to use NgbModalRef but to use NgbActiveModal with the suggestions that the guys above have suggested. 因此,解决此问题的方法不是使用NgbModalRef而是使用NgbActiveModal以及上述人员提出的建议。

Within the app.module.ts file you need to ensure that all components that contain a modal are within the entryComponents options and NgbModule.forRoot() in the imports options. app.module.ts文件中,您需要确保包含模态的所有组件都在entryComponents选项中,并且在imports选项中是NgbModule.forRoot()

@NgModule({
    imports: [
        NgbModule.forRoot()
    ],
    entryComponents: [
        AccountManagementComponent, 
        CompetitionManagementComponent
    ]
})

Within the component you need to import NgbActiveModal 在组件内,您需要导入NgbActiveModal

import {NgbModal, NgbActiveModal} from "@ng-bootstrap/ng-bootstrap";

and then create a variable 然后创建一个变量

activeModal: NgbActiveModal;

Create a function that will act as your open() 创建一个将充当您的open()的函数

open(content: any) {
    this.activeModal = this.modalService.open(content,{ ariaLabelledBy: "modal-basic-title", backdrop: "static", keyboard: false });
}

In our case we have a form in the modal so this needs to save and then if the save is successful it will close the modal and display a msg on the main page. 在我们的例子中,我们在模态中有一个表单,因此需要保存,然后,如果保存成功,它将关闭模态并在主页上显示一个消息。

save(model: any): void {
        this.dbService.save(model).subscribe(
            data => {
                if (data.Successful === false) {
                    this.modelMsg = data.Information;
                } else {
                    this.msg = data.Information;
                    this.activeModal.close();
                }

            },
            error => this.msg = <any>error);
    } 

I hope this helps others that come across this issue. 我希望这对遇到此问题的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM