我是Angular的新手,在启动Bootstrap切换和Modal时遇到一些问题。 BootStrap CSS文件之所以有效,是因为表单采用了样式。但是,我认为JS文件未正确链接。请让我知道我在做什么。
我使用Angular CLI进行了BootStrap和jQuery的NPM安装。 安装后,我将BootStrap和jQuery文件链接到angular.json文件,以便应用程序能够使用这些库。
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/LyndaChapter1",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
**"styles": [
**"node_modules/bootstrap/dist/css/bootstrap.css",
"src/styles.css"**
],
"scripts": [
"node_modules/bootstrap/dist/js/bootstrap.js",
"node_modules/jquery/dist/jquery.js"
],**
"es5BrowserSupport": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
预期结果:切换和模态应该起作用 实际结果:都不起作用
答案 0 :(得分:1)
虽然我同意此处关于不使用jQuery的其他答案,但是,如果您打算将其与引导程序或依赖的任何其他库一起使用,则必须先加载jQuery 。
因此,要解决您的问题,只需将jQuery依赖项放在引导程序依赖项之上。
答案 1 :(得分:0)
尝试将这些软件包添加到package.json文件,然后在Package.json文件级别运行npm install --save命令。
/ *“ @ ng-bootstrap / ng-bootstrap”:“ ^ 4.1.0”, “ bootstrap”:“ ^ 4.3.1” * /
答案 2 :(得分:0)
jQuery是在Angular上运行的错误选择……您可能需要的是ngBootstrap
那里有出色的样板代码,您可以像i did here一样使用
相关的 HTML :
<ng-template #content let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Profile update</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">
<form>
<div class="form-group">
<label for="dateOfBirth">Date of birth</label>
<div class="input-group">
<input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
</div>
</ng-template>
<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>
<hr>
<pre>{{closeResult}}</pre>
<hr/>
<p>
<button type="button" class="btn btn-outline-primary" (click)="isCollapsed = !isCollapsed"
[attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
Toggle
</button>
</p>
<div id="collapseExample" [ngbCollapse]="isCollapsed">
<div class="card">
<div class="card-body">
You can collapse this card by clicking Toggle
</div>
</div>
</div>
相关的 TS :
import {Component} from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ngbd-modal-basic',
templateUrl: './modal-basic.html'
})
export class NgbdModalBasic {
closeResult: string;
public isCollapsed = false;
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}`;
}
}
}