在Bootstrap升级3到4之后,工作Angular NavBar无法正常工作

时间:2018-01-23 10:27:45

标签: angular bootstrap-4

我在Angular4中使用Bootstrap 3.3.7开发了一个简单的NavBar。当我尝试升级到Bootstrap 4(4.0.0-beta或4.0.0-alpha.6)时,NavBar不再显示在浏览器中(控制台中也没有显示错误)。

HTML:

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li routerLinkActive="active current"><a routerLink="/messages"><b>Messages</b></a></li>
        <li routerLinkActive="active current"><a routerLink="/transformations"><b>Transformations</b></a></li>
        <li routerLinkActive="active current"><a routerLink="/actions"><b>Actions</b></a></li>
        <li routerLinkActive="active current"><a routerLink="/rules"><b>Rules</b></a></li>
      </ul>
    </div>
  </div>
</nav>

打字稿:

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

@Component({
  selector: 'navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

非常感谢任何帮助,谢谢,马丁

2 个答案:

答案 0 :(得分:2)

导航栏未显示的原因是因为Bootstrap 4几乎是完全重写的Bootstrap 3.换句话说,这两个版本完全不兼容。

以下是Bootstrap 4中基本导航栏结构和组件的示例:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<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 class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </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>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

另请注意下面的各种JavaScript组件。这些对于使导航栏工作至关重要。确保加载它们。按此顺序。

对于您的特定导航栏,HTML将是:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-light bg-light">
   <div class="container">
       <a class="navbar-brand" href="#">NavbarBrand</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" routerLinkActive="active current">
                   <a class="nav-link" routerLink="/messages"><b>Messages</b></a>
               </li>
               <li class="nav-item" routerLinkActive="active current">
                   <a class="nav-link" routerLink="/transformations"><b>Transformations</b></a>
               </li>
               <li class="nav-item" routerLinkActive="active current">
                   <a class="nav-link" routerLink="/actions"><b>Actions</b></a>
               </li>
               <li class="nav-item" routerLinkActive="active current">
                   <a class="nav-link" routerLink="/rules"><b>Rules</b></a>
               </li>
           </ul>
       </div>
   </div>
</nav>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

P.S。您应该避免将container-fluid类用于导航栏,因为宽4K屏幕上的用户会对您感到生气。请改用container(如我上一个代码段所示)。

答案 1 :(得分:0)

导航栏结构和类名已从Bootstrap 3.x更改为4.x.您需要migrate the 3.x code accordingly

http.get(baseUrl + 'api/Student/Students1')
.subscribe(result:Students => {
    this.std.push(result);
}, error => console.error(error));

https://www.codeply.com/go/RIbYHcOnQJ

详细了解what's changed and upgrade from 3.x to 4.x