无法在Angular中的Bootstrap 4中切换Navbar

时间:2017-08-30 03:17:41

标签: twitter-bootstrap angular navbar bootstrap-4 togglebutton

为什么我无法使用bootstrap 4在角度4中调整大小到移动屏幕时切换导航栏。我将它包含在脚本和样式的角度cli中,来自bootstrap的节点模块。我的代码中是否缺少某些内容?请检查下面。有什么不对?请帮忙

<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="#">Dashboard</a>
  <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <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="#">Settings</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Profile</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Help</a>
      </li>
    </ul>
    <form class="form-inline mt-2 mt-md-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>
  

.angular-cli.json

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

    "../node_modules/popper.js/dist/umd/popper.min.js",
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js"

  ],

的package.json

      {
  "name": "dashboard2",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "bootstrap": "^4.0.0-beta",
    "core-js": "^2.4.1",
    "jquery": "^3.2.1",
    "popper.js": "^1.12.5",
    "rxjs": "^5.4.1",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.2.7",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
}

11 个答案:

答案 0 :(得分:34)

看起来您可能一直在查看Bootstrap中的this示例。我做了,也有同样的问题。

问题是它不是一个有角度的例子,所以它不起作用。要使其工作,您必须使用(click)事件和变量。所以将模板更改为

<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="#">Dashboard</a>
  <button class="navbar-toggler d-lg-none" type="button" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="navbarsExampleDefault" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault" [ngbCollapse]="isCollapsed">
    <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="#">Settings</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Profile</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Help</a>
      </li>
    </ul>
    <form class="form-inline mt-2 mt-md-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

如果您希望在默认情况下折叠菜单,则应在类中将变量设置为true(在组件的.ts文件中)public isCollapsed = true;

这是plunker

还有一件事;如果导航器位于共享模块中,则必须记住在此处导入NgbCollapseModule。

也就是说,你的shared.module.ts应该是:

import { NgModule } from '@angular/core';
import { NgbCollapseModule } from '@ng-bootstrap/ng-bootstrap';
/*Plus all your other modules*/

@NgModule({
  imports: [NgbCollapseModule],
  declarations: [],
  exports: []
})
export class SharedModule {}

答案 1 :(得分:12)

@Joseph,看起来你还没有安装bootstrap(和bootstrap本身)的依赖项,导航栏切换工作。

如果打开终端或命令行并导航到包含package.json文件的目录,请运行以下命令:

npm install --save jquery
npm install --save popper.js
npm install --save bootstrap@4.0.0-beta

这些命令将为您的项目安装必需的依赖项。他们在他们正在做的事情上相当明确,但如果您需要更新他们正在做的事情或他们的工作方式,我建议您查看npm install位于import UIKit import Eureka class ViewController: FormViewController { //Creating Global Variables var name: String = "" var data: Date? = nil @IBAction func testebutton(_ sender: Any) { print(name) print(data) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. form +++ Section() <<< TextRow() { row in row.title = "Name" row.tag = "name" } <<< DateRow() { $0.title = "Birthdate" $0.value = Date() $0.tag = "date" } //Gets value from form let row: TextRow? = form.rowBy(tag: "name") let nome = row?.value name = nome! } 的文档{3}}。

安装完成后,您需要确保将它们包含在.angular-cli.json文件中,这样您的角度应用就可以使用它们。

答案 2 :(得分:8)

此示例提供汉堡包图标的切换功能(换句话说,如果它在响应模式下运行,则为整个导航栏)以及切换下拉菜单项。

它使用ng-bootstrap但我更愿意使用本机Bootstrap导航栏支持,而不是做这个“hacks”!?

显然,其他人也在努力解决同一问题:https://github.com/twbs/bootstrap/issues/24227

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">ApplicationName</a>
  <button class="navbar-toggler" type="button" (click)="toggleNavbar = !toggleNavbar">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" [ngbCollapse]="!toggleNavbar">
    <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="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li ngbDropdown class="nav-item">
        <a class="nav-link" id="navbarDropdownMenuLink" ngbDropdownToggle aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div ngbDropdownMenu aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

答案 3 :(得分:2)

如前所述,您需要一个属性(例如isShown,最初为false) 然后使用

[ngClass]="{ 'show': isShown }" 

此处

<div class="collapse navbar-collapse" [ngClass]="{ 'show': isShown }" id="navbarSupportedContent" >

上的链接点击事件,确保当一个链接后面的菜单中再次崩溃

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

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


  isShown:boolean = false;
  constructor() { }

  ngOnInit() {
  }
  
}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" routerLink="/"><img width="20%" src="/assets/logo.svg"></a>
  <button #navbarToggler class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" (click)="isShown = !isShown" [attr.aria-expanded]="isShown"  aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" [ngClass]="{ 'show': isShown }" id="navbarSupportedContent" >
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" (click)="isShown = false" routerLink="/">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" (click)="isShown = false" routerLink="/contact">Contact</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" (click)="isShown = false" routerLink="/imprint">Imprint</a>
      </li>
    </ul>
  </div>
</nav>

答案 4 :(得分:1)

要使切换开关在角度模式下工作,您只需要做4件事

  1. 安装ng boostrap
  2. 将切换点击监听器添加到导航栏(click)="isCollapsed = !isCollapsed"
  3. use属性绑定到aria展开的[attr.aria-expanded]="!isCollapsed" 这样,当您单击(真或假)时,该属性将会更改
  4. 使用属性绑定到ngCollapse [ngbCollapse]="isCollapsed"
  5. 的方法相同
  

注意:不建议使用JQuery

Collapse nav bar without using JQuery

答案 5 :(得分:1)

如果有人正在寻找一种动态切换导航栏的方法 您可以将ViewChild与模板变量一起使用以获取对按钮的引用,然后可以触发click事件:

HTML

<button #menu class="navbar-toggler d-lg-none" type="button"
    data-toggle="collapse" data-target="#navbarsExampleDefault"
    aria-controls="navbarsExampleDefault" aria-expanded="false"
    aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>

TS

import { ViewChild, ElementRef } from '@angular/core';

@ViewChild('menu') menu: ElementRef;

toggleMenu() {
    this.menu.nativeElement.click();
}

closeMenu() {
    const isMenuOpen = this.menu.nativeElement.getAttribute('aria-expanded') === 'true';
    if (isMenuOpen) {
        this.menu.nativeElement.click();
    }
}

我知道这不是对这个问题的答案,但它与之息息相关,可能会有所帮助。

答案 6 :(得分:1)

尝试这个绝对好用

<nav class="navbar navbar-expand-md navbar-light">
  <a>Home</a>
  <button
    class="navbar-toggler d-lg-none"
    type="button"
    data-toggle="collapse"
    (click)="isShown=!isShown"
  >
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" [ngClass]="{ 'show': isShown }">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
  </div>
</nav>

对应的ts文件是

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  // title = 'WebApp';
  isShown = false;
}

答案 7 :(得分:0)

如果您想使Angular与Bootstrap 4一起使用而不使用@ ng-bootstrap,请参见以下git repo中的代码:https://github.com/fmorriso/Angular-bootstrap4-nav/tree/master/src/app/navigation

我为此花了太多时间。您必须破解Angular才能使其正常工作;这不仅仅是版本不正确的问题,等等。

答案 8 :(得分:0)

这对我有用!

现在,当使用引导程序4以角度4调整大小到移动屏幕时,我们可以切换导航栏。

尝试此代码

app.component.html

<nav class="navbar navbar-expand-lg justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" id="navButton">
    <i class="fa fa-bars"></i>
  </button>
  <div class="row">
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item" *ngFor="let menu of mainmenu, let i = index">
          <a class="nav-link" (click)="findIndex(i)" routerLink="/{{menu.href}}">{{menu.icon}} {{menu.text}}</a>
          <ng-container *ngIf="menu.children && menu.children.length > 0">
            <span class="d-block d-sm-none"><i class="fa fa-chevron-down" aria-hidden="true"></i></span>
          </ng-container>
          <ul class="sub_menu dropdown-menu" [ngClass]="{active : isActive(i)}" *ngIf="menu.children && menu.children.length > 0">
            <li *ngFor="let sub_menu of menu.children"><a class="nav-link" routerLink="/{{sub_menu.href}}" (click)="closeMenu()"><img src="{{sub_menu.icon}}" class="nav-img" /> {{sub_menu.text}}</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>

app.component.ts

closeMenu() {
    var isMobile = /iPhone|iPad|iPod|BlackBerry|Opera Mini|IEMobile|Android/i.test(navigator.userAgent);
    if (isMobile) {
      document.getElementById('navButton').click();
   }
}

答案 9 :(得分:0)

我不知道这是否仍然有用,但是当我遇到此问题时对我有所帮助的是停止实时服务器,然后重新编译应用程序,然后使用ng serve重新启动它。有时,当您在服务器运行时安装依赖项时,即使插入依赖项,angular.json也不会看到它们。也许是将来的用户

答案 10 :(得分:0)

Bootstrap-4.4.1,Angular-cli:7.3.9

最简单的方法是在您的客户端入口点中导入js捆绑包:

导入'bootstrap / dist / js / bootstrap.bundle';行到“ polyfills.ts”文件中