本文介绍了Bootstrap 4 导航栏不会在 Angular4 中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Angular 4 项目中使用 Bootstrap 4 和 Ng-Bootstrap,

我以这种方式在我的 angular-cli.json 中链接了 que bootstrap.css

 "样式": ["../node_modules/bootstrap/dist/css/bootstrap.css",样式.css"],

并在我的 app.module 中导入 NgbModule,如下所示

import { BrowserModule } from '@angular/platform-b​​rowser';从'@angular/core' 导入 { NgModule };从'@ng-bootstrap/ng-bootstrap'导入{NgbModule};从 './app.component' 导入 { AppComponent };@NgModule({声明: [应用组件],进口:[浏览器模块,NgbModule.forRoot()],提供者:[],引导程序:[AppComponent]})导出类 AppModule { }

我制作了一个导航栏,从 bootstrap 网站复制了文档,并从 ng-bootstrap 添加了切换组件(效果很好)代码看起来像这样

 <a class="navbar-brand" href="#">Navbar</a><button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-展开=假"aria-label=切换导航"><span class="navbar-toggler-icon"></span><div class="collapse navbar-collapse" id="navbarSupportedContent"><ul class="navbar-nav mr-auto"><li class="nav-item active"><a class="nav-link" href="#">首页 <span class="sr-only">(当前)</span></a><li class="nav-item"><a class="nav-link" href="#">Link</a><li><div ngbDropdown class="d-inline-block"><div class="nav-link" id="dropdownBasic1" ngbDropdownToggle>切换下拉菜单

<div ngbDropdownMenu aria-labelledby="dropdownBasic1"><button class="dropdown-item">Action - 1</button><button class="dropdown-item">另一个动作</button><button class="dropdown-item">这里还有别的东西</button>

<li class="nav-item"><a class="nav-link disabled" href="#">Disabled</a><form class="form-inline my-2 my-lg-0"><input class="form-control mr-sm-2" type="search" placeholder="Search"咏叹调标签=搜索"><button class="btn btn-outline-success my-2 my-sm-0"type="submit">搜索</button></表单>

</nav>

当我缩小窗口并且切换起作用时,问题就开始了,当我按下用于折叠导航栏的按钮时它不起作用.

我尝试安装和链接 jquery 和 popper 的 js 文件以及引导程序,但它仍然不起作用,实际的 angular-cli.json 看起来像这样

 "样式": ["../node_modules/bootstrap/dist/css/bootstrap.css",样式.css"],脚本":["../node_modules/popper.js/dist/umd/popper.min.js","../node_modules/jquery/jquery.min.js",../node_modules/bootstrap/dist/js/bootstrap.min.js"],

我做错了什么?

解决方案

要使用 angular,您需要此修复程序

首先在你的类中定义它public navbarCollapsed = true;

然后更改您的按钮

<button class="navbar-toggler" type="button" (click)="navbarCollapsed = !navbarCollapsed" [attr.aria-expanded]="!navbarCollapsed" aria-controls="navbarContent"aria-expanded="false" aria-label="切换导航"><span class="navbar-toggler-icon"></span>

您还需要将此添加到折叠的 div

现在你不需要 jquery、popper 或 bootstrap.js

您可以在此处找到有关此问题的更多信息.>

还要检查 ng-bootstraps 工作演示页面代码 链接.

I'm working with Bootstrap 4 and Ng-Bootstrap in an Angular 4 Project,

i have linked the que bootstrap.css in my angular-cli.json this way

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

and imported the NgbModule in my app.module that looks like this

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

i made a Navbar copying the documentation from the bootstrap website and added the toggle component from ng-bootstrap (it works well) the code looks like this

    <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>
        <div ngbDropdown class="d-inline-block">
          <div class="nav-link" id="dropdownBasic1" ngbDropdownToggle>Toggle
dropdown</div>
          <div ngbDropdownMenu aria-labelledby="dropdownBasic1">
            <button class="dropdown-item">Action - 1</button>
            <button class="dropdown-item">Another Action</button>
            <button class="dropdown-item">Something else is here</button>
          </div>
        </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>

the problem starts when i scale down the window and the toggle does its work, when i press the button for collapsing the navbar it doesn't work.

i tried installing and linking the js files of jquery and popper and the bootstrap one, but it still doesn't work, the actual angular-cli.json looks like this

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

      ],

what am i doing wrong?

解决方案

To work with angular you need this fix

first define this in your class public navbarCollapsed = true;

then chnage your button

<button class="navbar-toggler" type="button" (click)="navbarCollapsed = !navbarCollapsed" [attr.aria-expanded]="!navbarCollapsed" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>

also you need to add this to your collapsing div

<div class="collapse navbar-collapse" [ngbCollapse]="navbarCollapsed" id="navbarSupportedContent">

now you don't need jquery, popper or bootstrap.js

you can find more info about this issue here.

also check ng-bootstraps working demo page code link.

这篇关于Bootstrap 4 导航栏不会在 Angular4 中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:54