如何使用引导导航栏和下拉菜单(角度7)?

时间:2019-01-14 16:49:08

标签: angular bootstrap-4 navbar

我尝试了一下,但是当我单击Dropdown link nemu时,什么也没发生。

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a class="nav-link" routerLink="dashboard">dashboard</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="calendar">calendar</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="manager">manager</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" id="navbardrop" data-toggle="dropdown">
                    Dropdown link
                </a>
                <div class="dropdown-menu">
                    <a class="dropdown-item" href="#">Link 1</a>
                    <a class="dropdown-item" href="#">Link 2</a>
                    <a class="dropdown-item" href="#">Link 3</a>
                </div>
            </li>
        </ul>
        <!--ul class="navbar-nav navbar-right">

        </ul-->
    </div>
</nav>

编辑:

我使用https://ng-bootstrap.github.io/#/components/datepicker/overviewnavbar不存在。

4 个答案:

答案 0 :(得分:2)

我在ng-bootstrap文档中发现navbar不存在。所以我添加了jQuery,它就可以工作。

npm install bootstrap --save
npm install jquery --save

并将其添加到angular.json(Angular 7):

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

答案 1 :(得分:1)

可能您不应在项目中包含bootstrap.min.js(jquery和popper.js也是如此)。

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

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

答案 2 :(得分:1)

如果您不想在项目中添加 jquery,pooper和bootstrap JS ,则可以这样做,我添加了navbar和btn-group的示例

解释

show class is use to show menu of dropdown

当您点击 anchor 标记时, show 变量切换且其中包含布尔类型(在 .ts 文件中指定)。在 div.dropdown菜单时检查 show 类,如果其为false,则无法显示,反之亦然。

app.component.html

  

在导航栏中

<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">
        <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="javascript:void(0)">Home</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="javascript:void(0)">List 1</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="javascript:void(0)">List 2</a>
            </li>
        </ul>
        <ul class="navbar-nav">
            <li class="nav-item dropdown ml-auto">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" data-toggle="dropdown" (click)="show=!show">Dropdown</a>
                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown" [ngClass]="{show: show === true}">
                    <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>
        </ul>
    </div>
</nav>
  

在btn组中

<div class="btn-group">
    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" (click)="show=!show">Action</button>
    <div class="dropdown-menu" [ngClass]="{show: show === true}">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
    </div>
</div>

app.component.ts

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

@Component({
    selector: 'app-header',
    templateUrl: './header.component.html',
    styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
    show: boolean = false;

    constructor() {}

    ngOnInit() {}

}

答案 3 :(得分:0)

它不起作用,因为我认为popper.js存在问题,我遇到了同样的问题,但是我能够解决它...在您的angular.json中尝试

导入脚本

"./node_modules/jquery/dist/jquery.js",
"./node_modules/bootstrap/dist/js/bootstrap.bundle.js"

它应该工作