Bootstrap Navbar无法正常工作:AngularCLI

时间:2018-02-11 19:44:34

标签: angular twitter-bootstrap angular-cli

我将bootstrap包含在我的angularcli项目中,但是当我尝试包含导航栏时,没有任何元素正常工作。我在网站上使用标准的boostrap导航栏示例,但看起来很奇怪:

enter image description here

我的项目导致导航栏无法正确加载有什么问题?

navbar.component.html

enter image description here



<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" 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>
  <a class="navbar-brand" href="#">Navbar</a>

  <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 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="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>
&#13;
&#13;
&#13;

.angular-cli.json

&#13;
&#13;
"styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "styles.css"
      ],
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:7)

下面的代码显示了如何使用ng-bootstrap和Bootstrap 4在Angular中实现导航栏。您可以在this plunker中看到它。

  • ng-bootstrap库是为Angular开发的,不需要jQuery
  • 使用Bootstrap 4,replace navbar-toggleable-md navbar-expand-md
  • ngbCollapse指令用于显示/隐藏下拉菜单
<nav class="navbar navbar-expand-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" (click)="isNavbarCollapsed = !isNavbarCollapsed" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div [ngbCollapse]="isNavbarCollapsed" class="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 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="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

答案 1 :(得分:5)

bootstrap 4中存在一些已知问题,尤其是在navbar实现中

因此你必须安装bootstrap bootstrap@3.3.7

如果您使用的是npm,则最好推荐以下库

 npm install bootstrap@3.3.7 --save

npm install bootstrap@3.3.7 tether jquery --save

在.angular-cli.json文件中添加依赖项

"styles":[
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css"
],

"scripts":[
"../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"
],

使用以下HTML

 <nav class="navbarnavbar-inverse">
    <div class="container">
    <div class="navbar-header">
    <button type="button" class="navbar-togglecollapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
    <span class="sr-only">Togglenavigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="#">Angular5Website</a>
    </div>
    <div id="navbar" class="collapsenavbar-collapse">
    <ul class="navnavbar-nav">
    <li class="active"> <a href="#">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>
    </ul>
    </div><!--/.nav-collapse-->
    </div>
    </nav>

答案 2 :(得分:1)

navbar.component.html:

<nav class="navbar navbar-default">
   <div class="container-fluid">
      <div class="navbar-header">
         <button type="button" class="navbar-toggle" (click)="toggleNavbarCollapsing()">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
         </button>
         <a routerLink="/" class="navbar-brand">Navbar</a>
      </div>
      <div class="navbar-collapse" [class.collapse]="navbarCollapsed">
      ...

navbar.component.ts:

navbarCollapsed = true;

toggleNavbarCollapsing() {
    this.navbarCollapsed = !this.navbarCollapsed;
}