Bootstrap 4 navbar is not working with angular 5

2019-07-27 00:14发布

问题:

Hi, I am trying to add bootstrap with angular project and the styling is not reflecting in the browser.

  The details are as below.
Versions:
Angular CLI: 1.7.3
Node: 9.7.1
OS: win32 x64
Angular: 5.2.8

installed bootstrap and jquery using angular cli

"bootstrap": "^4.0.0"
"jquery": "^3.3.1"

in .angular-cli.json added as below

  "styles": [        
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js"
  ],

Below is the code in my html.

<nav class="navbar navbar-default">
  <div class="container-fluid">
      <div class="navbar-header">
         <span class="navbar-brand">Angular + Bootstrap</span>
      </div>
      <ul class="nav navbar-nav">
          <li class="active"><a href="#">
            Link <span class="sr-only">(current)</span>
          </a></li>
          <li><a href="#">Link</a></li>
          <li class="dropdown" dropdown> <!-- {1} -->
              <a dropdownToggle role="button"> <!-- {2} -->
                Dropdown <span class="caret"></span></a>
              <ul *dropdownMenu class="dropdown-menu"> <!-- {3} -->
                  <li><a href="#">Action</a></li>
                  <li><a href="#">Another action</a></li>
                  <li><a href="#">Something else here</a></li>
                  <li role="separator" class="divider"></li>
                  <li><a href="#">Separated link</a></li>
                  <li role="separator" class="divider"></li>
                  <li><a href="#">One more separated link</a></li>
              </ul>
          </li>
      </ul>
  </div>
</nav>

I am getting the view in browser as below and not getting any errors in the browser console. output in browser

Thanks for your help in advance.

回答1:

you have installed bootstrap 4 and using bootstrap 3 version navbar code. here the sample code for bootstrap 4 navbar.Some classes has been changed in latest bootstrap 4 version.

<nav class="navbar navbar-expand-sm bg-light">
  <ul class="navbar-nav">
    <li class="nav-item">
      <a class="nav-link" href="#">Link 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 2</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Link 3</a>
    </li>
  </ul>
</nav>

for more info refer this link



回答2:

Try this

Go to app folder --> src

Open style.css and paste below code

@import "~bootstrap/dist/css/bootstrap.css";