NgbModule.forRoot() ให้ข้อผิดพลาดคุณสมบัติ 'forRoot' ไม่มีอยู่ในประเภท 'typeof NgbModule'.ts

ฉันต้องการเพิ่ม Modal Bootstrap ให้กับ Angular Project ของฉัน ดังนั้นฉันจึงติดตั้ง ng bootstrap โดยใช้ npm install --save @ng-bootstrap/ng-bootstrap จากนั้นบูตสแตรปโดยใช้ npm install [email protected]

เมื่อฉันเพิ่มประโยคนี้ NgbModule.forRoot() ภายใต้การนำเข้าใน app.module.ts มันทำให้เกิดข้อผิดพลาด Property 'forRoot' ไม่มีอยู่ในประเภท 'typeof NgbModule'

อย่างไรก็ตาม ฉันพยายามลบ forRoot() และนำเข้าเฉพาะ NdbModule และรันแอปพลิเคชันของฉัน แต่มันให้ข้อผิดพลาดดังต่อไปนี้

ป้อนคำอธิบายรูปภาพที่นี่

เมื่อฉันค้นหาทางออนไลน์เกี่ยวกับวิธีลบข้อผิดพลาดข้างต้น วิธีแก้ไขที่ฉันได้รับคือเพิ่ม forRoot() แต่มันกำลังให้ข้อผิดพลาดคุณสมบัติ 'forRoot' ไม่มีอยู่ในประเภท 'typeof NgbModule'

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

เข้าสู่ระบบ.component.ts:

import { Component, OnInit } from '@angular/core';

import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  title = 'appBootstrap';
  
  closeResult: string;
  
  constructor(private modalService: NgbModal) {}
    
  open(content) {
    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }
  
  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return  `with: ${reason}`;
    }
  }

  ngOnInit() {
    
  }
}

เข้าสู่ระบบ.component.html:

<h1>Modal Popup Example</h1>
   
<button class="btn btn-lg btn-outline-primary" (click)="open(mymodal)">Open My Modal</button>
   
<ng-template #mymodal let-modal>
  <div class="modal-header">
    <h4 class="modal-title" id="modal-basic-title">Bootstrap Modal</h4>
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
      <span aria-hidden="true">×</span>
    </button>
  </div>
  <div class="modal-body">
    This is a sample modal
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Ok</button>
  </div>
</ng-template>

ดัชนี.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Testproject</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
  <app-root></app-root>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>

เวอร์ชันเชิงมุม: Angular CLI: 8.0.6 โหนด: 12.19.0 ระบบปฏิบัติการ: win32 x64 Angular: 8.0.3

นี่เป็นครั้งแรกที่ฉันโพสต์คำถามใน Stackoverflow โปรดแนะนำหากฉันพลาดรายละเอียดใด ๆ


person Mounika CH    schedule 20.11.2020    source แหล่งที่มา
comment
คุณใช้ ngBoostrap เวอร์ชันใดอยู่   -  person Aditya Menon    schedule 20.11.2020
comment
จาก (ฉันคิดว่าจำได้) ng-bootstrap 4.0.0 ไม่ได้ใช้ forRoot() เวอร์ชันที่แท้จริงของ ng-bootstrap คือ 8.0.0   -  person Eliseo    schedule 20.11.2020
comment
Angular: 8.0.0 Bootstrap: 4.5.2 ฉันติดตั้ง ng bootstrap โดยใช้ npm install --save @ng-bootstrap/ng-bootstrap ดังนั้นเวอร์ชันควรเป็นเวอร์ชันล่าสุดซึ่ง 8.0.0   -  person Mounika CH    schedule 20.11.2020


คำตอบ (2)


ดูเหมือนว่าจะเป็นปัญหาความเข้ากันได้ระหว่าง Angular, Bootstrap และ NgBootstrap โปรดตรวจสอบให้แน่ใจว่าคุณใช้ การอ้างอิง ที่ถูกต้องตาม เอกสาร NgBootstraps

person Aditya Menon    schedule 20.11.2020
comment
Angular: 8.0.0 Bootstrap: 4.5.2 ฉันติดตั้ง ng bootstrap โดยใช้ npm install --save @ng-bootstrap/ng-bootstrap ดังนั้นเวอร์ชันควรเป็นเวอร์ชันล่าสุดซึ่ง 8.0.0 - person Mounika CH; 20.11.2020
comment
นั่นคือปัญหาอย่างชัดเจน หากคุณดูตารางในลิงก์ด้านบน คุณจะเห็นว่าสำหรับ Angular 8 คุณต้องใช้บิวด์ล่าสุดภายในเวอร์ชัน 5 (5.x.x หมายถึงเวอร์ชันใดก็ได้ของ 5) และใช้ Bootstrap 4.3.1 - person Aditya Menon; 20.11.2020
comment
คุณช่วยบอกวิธีการติดตั้ง ng bootstrap เวอร์ชัน 5.x.x ให้ฉันหน่อยได้ไหม ฉันค้นหาออนไลน์และพบว่าเวอร์ชันเชิงมุม>= 9 แล้ว ng add @ng-bootstrap/ng-bootstrap ใช้เพื่อติดตั้งและติดตั้งเวอร์ชันล่าสุด แต่จะติดตั้ง nb bootstrap เวอร์ชั่น 5.x.x ได้อย่างไร - person Mounika CH; 21.11.2020
comment
ใช้ npm i @ng-bootstrap/[email protected] นี่คือเวอร์ชันล่าสุดปัจจุบันภายใต้ร่ม 5.x.x - person Aditya Menon; 22.11.2020

ฉันมีปัญหาเดียวกันขณะพยายามติดตั้ง bootstrap สำหรับโปรเจ็กต์เชิงมุมของฉันที่สร้างโดยใช้ angular/cli

ฉันปฏิบัติตามวิธี 'การติดตั้งด้วยตนเอง' ที่กล่าวถึงใน https://ng-bootstrap.github.io/#/getting-started (คลิกที่ลิงก์ คลิกที่นี่เพื่อซ่อนคำแนะนำโดยละเอียดสำหรับคำแนะนำทีละขั้นตอน)

สิ่งสำคัญคือการตรวจสอบการขึ้นต่อกัน - ng bootstrap dependencies


คำอธิบายของสิ่งที่ฉันทำ:

  1. ตามที่กล่าวไว้ที่นี่ (ng bootstrap dependencies) สำหรับ Angular 10 การขึ้นต่อกันคือ ng-bootstrap - 8.x.x และ Bootstrap CSS - 4.5.0

    npm install [email protected]
    npm install @ng-bootstrap/[email protected]
    

    หมายเหตุ: ใช้สวิตช์ --legacy-peer-deps หากคุณได้รับข้อผิดพลาดในการขึ้นต่อกัน

  2. เนื่องจากฉันใช้ SCSS จึงเพิ่มบรรทัดด้านล่างใน src/styles.scss

    @import "~bootstrap/scss/bootstrap";
    
  3. เพิ่ม ng-localize

    ng add @angular/localize
    
  4. เกี่ยวกับ NgbModule.forRoot() ฉันสามารถยืนยันได้ว่าไม่จำเป็นใน ng-bootstrap เวอร์ชันใหม่กว่า (ฉันลอง 8.0.0) เนื่องจากฉันมี bootstrap ทำงานในโปรเจ็กต์เชิงมุมของฉันโดยไม่มีมันดังที่แสดงด้านล่าง:

    ไฟล์: app.module.ts

    import {NgbModule.NgbModule} from '@ng-bootstrap/ng-bootstrap';
    
    @NgModule({
      declarations:[AppComponent],
      imports: [NgbModule],
      providers: [],
      bootstrap: [AppComponent]
    })
    
    export class AppModule {
    }
    

person Renju Jose    schedule 20.05.2021