Angular 4 Bootstrap alpha Navigation Bar not rendering properly

时间:2017-10-12 10:18:34

标签: angular bootstrap-4

I'm trying to add the Bootstrap 4 to Angular 4 application. I'm following this tutorial. But the navigation bar is not working properly.

enter image description here

I have followed following steps to install Bootstrap.

First I installed the Bootstrap using following npm command.

npm install bootstrap@next --save

enter image description here

I can see the bootstrap folder in the node_modules folder.

enter image description here

Then I included the bootstrap css path in the global css file.

/* You can add global styles to this file, and also import other style files */
@import "~bootstrap/dist/css/bootstrap.css";

body {
    padding-top: 5rem;
}
.starter-template {
    padding: 3rem 1.5rem;
    text-align: center;
}

Then I opened the index.html file and did add the JQuery and other JS files at the end of the file.

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<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.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>

I did run the server it loads the Bootstrap CSS. But navigation bar is not rendering properly. I did the hard reload to clear the cache.

enter image description here

My app.component.html file is,

<div class="container">

  <div class="starter-template">
    <h1>Bootstrap starter template</h1>
    <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
  </div>

</div><!-- /.container -->

app-navbar.componenet.html file,

<nav class="navbar">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Project name</a>
    </div>
    <div id="navbar" class="collapse navbar-collapse">
      <ul class="nav navbar-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>

Console shows that Bootstrap CSS and JS files loading properly.

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

从图像看起来你已经安装了最新版本的bootstrap 4,这是测试版,但是你拥有的代码是alpha版本,试试看它应该有用:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
        <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>
        <a class="navbar-brand" href="#">Project name</a>

        <div id="navbar" class="collapse navbar-collapse">
            <ul class="navbar-nav">
                <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="#">About </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact </a>
                </li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </nav>

Official docs