Angular/Bootstrap - 导航栏无法正确显示
Posted
技术标签:
【中文标题】Angular/Bootstrap - 导航栏无法正确显示【英文标题】:Angular/Boostrap - Navbar doesn't display properly 【发布时间】:2018-06-29 23:40:31 【问题描述】:最近我在我的项目中添加了 Bootstrap 4、jQuery 和 Popper,但问题是当我渲染我的网页时,它会显示这样的导航栏(它位于一个名为 header 的组件中):
我还在我的 angular-cli.json 中添加了:
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
"styles.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
HTML 代码
<nav class="navbar navbar-light bg-light">
<button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse"
data-target="#collapsibleNavId" aria-controls="collapsibleNavId"
aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="collapse navbar-expand-md" id="collapsibleNavId">
<a class="navbar-brand" href="#">My App/Logo with Text</a>
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Nav 1 <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Nav 2</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="dropdownId"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Nav 3 dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdownId">
<a class="dropdown-item" href="#">Action 1</a>
<a class="dropdown-item" href="#">Action 2</a>
</div>
</li>
</ul>
<form class="form-inline float-xs-right">
<input class="form-control" type="text" placeholder="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
【问题讨论】:
此代码远不足以进行故障排除。发布该页面的整个 html 输出。 从 Bootstrap 文档中提取的相同代码 您是说您正在使用从 Bootstrap 文档复制的代码并获得该结果? 是的,我认为它与 angular-cli.json 文件有关,而不是 HTML 本身,但我不知道。 【参考方案1】:您的导航栏中缺少一些类。查看以下代码以及其中使用的类和组件。如果您仍有疑问,请随时提问。但是该代码示例应该可以帮助您解决这个问题。
另外,d-lg-none
类说:“将其隐藏在大断点及以上。这是你想要的吗?这不是切换器的设计方式。引导程序被设计为 移动优先!这意味着切换器是默认设置。然后您使用navbar-expand-lg
类,它基本上说:“隐藏切换器并将导航栏从'lg'(=大)断点向上扩展为水平。”
这是我的代码示例:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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 class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<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>
<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>
<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.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
【讨论】:
webstorm 的引导片段是问题(它们都不完整) 您试图在代码中隐藏切换器,而正确的做法是相反,即使用navbar-expand-lg
类,它基本上说:“隐藏切换器并从“lg”(=大)断点开始”。这就是它的工作原理。以上是关于Angular/Bootstrap - 导航栏无法正确显示的主要内容,如果未能解决你的问题,请参考以下文章