问题
I'm working with Bootstrap 4 and Ng-Bootstrap in an Angular 4 Project,
i have linked the que bootstrap.css in my angular-cli.json this way
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"styles.css"
],
and imported the NgbModule in my app.module that looks like this
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgbModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
i made a Navbar copying the documentation from the bootstrap website and added the toggle component from ng-bootstrap (it works well) the code looks like this
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li>
<div ngbDropdown class="d-inline-block">
<div class="nav-link" id="dropdownBasic1" ngbDropdownToggle>Toggle
dropdown</div>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button class="dropdown-item">Action - 1</button>
<button class="dropdown-item">Another Action</button>
<button class="dropdown-item">Something else is here</button>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search"
aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0"
type="submit">Search</button>
</form>
</div>
</nav>
the problem starts when i scale down the window and the toggle does its work, when i press the button for collapsing the navbar it doesn't work.
i tried installing and linking the js files of jquery and popper and the bootstrap one, but it still doesn't work, the actual angular-cli.json looks like this
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"styles.css"
],
"scripts": [
"../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/jquery/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
what am i doing wrong?
回答1:
To work with angular you need this fix
first define this in your class public navbarCollapsed = true;
then chnage your button
<button class="navbar-toggler" type="button" (click)="navbarCollapsed = !navbarCollapsed" [attr.aria-expanded]="!navbarCollapsed" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
also you need to add this to your collapsing div
<div class="collapse navbar-collapse" [ngbCollapse]="navbarCollapsed" id="navbarSupportedContent">
now you don't need jquery, popper or bootstrap.js
you can find more info about this issue here.
also check ng-bootstraps working demo page code link.
回答2:
I had the same problem, I used normal bootstrap 4 rather then ng-bootstrap and this way I don't need to any module in app.module file as well. it works. Below is the script add into angular.json (angular v6) and bootstrap 4+:
"scripts": ["../node_modules/jquery/dist/jquery.js",
"../node_modules/popper.js/dist/umd/popper.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"]
For CSS I prefer to use Scss version so it goes like this.
"styles": [
"src/styles.scss",
"../node_modules/font-awesome/scss/font-awesome.scss"
],
and i import bootstrap into style.scss file
@import "~bootstrap/scss/bootstrap.scss";
Cheers! JK
回答3:
Look for the compiled classes, maybe you get the wrong version. I had a similar issue with the class col-sm-offset-#, the problem was that this classes doesn't exist in that version. Maybe could be the same issue
This is the route ..\node_modules\bootstrap\dist\js here we have two file bootstrap.js and bootstrap.min.js
find into bootstrap.js the nav classes. if you could not found it, look for another Bootstrap repository.
Good luck.
来源:https://stackoverflow.com/questions/47063068/bootstrap-4-navbar-doesnt-collapse-in-angular4