[英]Property 'forRoot' does not exist on type 'typeof NgbModule'.ts(2339)
[英]NgbModule.forRoot() giving error "Property 'forRoot' does not exist on type 'typeof NgbModule'.ts"
我想將 Bootstrap 模式添加到我的 Angular 項目中。 所以我使用“npm install --save @ng-bootstrap/ng-bootstrap”安裝了ng bootstrap,然后使用“npm install bootstrap@4.5.2”進行引導。
當我在 app.module.ts 中的導入下添加這句話“NgbModule.forRoot()”時,它給出了一個錯誤“屬性 'forRoot' 在類型 'typeof NgbModule' 上不存在。”
但是,我嘗試刪除 forRoot() 並僅導入 NdbModule 並運行我的應用程序。 但它給出了以下錯誤
當我在網上搜索如何消除上述錯誤時,我得到的解決方案是添加forRoot()。 但它給出了錯誤“類型'typeof NgbModule'上不存在屬性'forRoot'。”
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 版本:Angular CLI:8.0.6 節點:12.19.0 操作系統:win32 x64 Angular:8.0.3
這是我第一次在 Stackoverflow 中發布問題。 如果我錯過了任何細節,請提出建議。
似乎是 Angular、Bootstrap 和 NgBootstrap 之間的兼容性問題。 請確保根據 NgBootstraps 文檔使用正確的依賴項
我在嘗試為使用 angular/cli 創建的 angular 項目安裝引導程序時遇到了同樣的問題
我遵循了https://ng-bootstrap.github.io/#/getting-started中提到的“手動安裝”方法(單擊鏈接“單擊此處隱藏詳細說明”以獲取分步說明)
主要是檢查依賴項 - ng bootstrap 依賴項
我所做的解釋:
正如這里提到的( ng bootstrap 依賴項),對於 Angular 10,依賴項是ng-bootstrap - 8.xx
和Bootstrap CSS - 4.5.0
npm install bootstrap@4.5.0 npm install @ng-bootstrap/ng-bootstrap@8.0.0
注意:如果出現依賴錯誤,請使用--legacy-peer-deps
開關
由於我使用的是 SCSS,因此在src/styles.scss
添加了以下行
@import "~bootstrap/scss/bootstrap";
添加 ng-localize
ng add @angular/localize
關於NgbModule.forRoot()
,我可以確認在較新版本的 ng-bootstrap 中不需要它(我嘗試了 8.0.0),因為我的 angular 項目中沒有引導程序,如下所示:
文件:app.module.ts
import {NgbModule.NgbModule} from '@ng-bootstrap/ng-bootstrap'; @NgModule({ declarations:[AppComponent], imports: [NgbModule], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.