简体   繁体   English

Bootstrap 4导航栏不适用于角度路由器链接

[英]Bootstrap 4 navbar not working with angular router links

I have trouble setting up my router links on bootstrap 4 navbar syntax. 我在引导4导航栏语法上设置路由器链接时遇到问题。 Apparently they need href attributes to be able to click on them. 显然,他们需要href属性才能点击它们。 Is there any way to enable them anyway? 反正有什么方法可以启用它们?

<div class="collapse navbar-collapse">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a class="nav-link" routerLink="/dashboard" routerLinkActive="active">Dashboard</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="/charts" routerLinkActive="active">Charts</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="/map" routerLinkActive="active">Map</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="/about" routerLinkActive="active">about</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="/contact" routerLinkActive="active">Contact</a>
            </li>
        </ul>
</div> 

I have imported the styles and script into angular.json using the angular CLI latest. 我已经使用最新的angular CLI将样式和脚本导入了angular.json。

"styles": [
    "node_modules/bootstrap/dist/css/bootstrap.min.css",
    "node_modules/font-awesome/css/font-awesome.css",
    "src/styles.scss"
],
"scripts": [
    "node_modules/jquery/dist/jquery.slim.js",
    "node_modules/popper.js/dist/umd/popper.js",
    "node_modules/bootstrap/dist/js/bootstrap.min.js"
] 

You can try and run angular project. 您可以尝试运行角度项目。

  "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/font-awesome/css/font-awesome.css",
        "src/styles.scss"
    ],
"scripts": [
    "../node_modules/jquery/dist/jquery.slim.js",
    "../node_modules/popper.js/dist/umd/popper.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js"
] 

You can import the style in your root css file by @import "~bootstrap/dist/css/bootstrap.css"; 您可以通过@import“〜bootstrap / dist / css / bootstrap.css”将样式导入到您的根CSS文件中。

Try to avoid using bootstrap.js as this is built on jquery. 尝试避免使用bootstrap.js,因为它是基于jquery构建的。 Angular provides a package for using bootstrap.js you can use that. Angular提供了一个用于使用bootstrap.js的软件包。 Visit https://ng-bootstrap.github.io/#/getting-started to have an understanding of how to use Angular bootstrap module. 访问https://ng-bootstrap.github.io/#/getting-started以了解如何使用Angular bootstrap模块。

Happy coding, Cheers 快乐的编码,欢呼

I forgot to import RouterModule into the NavBarModule to make this work. 我忘了将RouterModule导入到NavBarModule中以使其工作。 I then had to change routerLink="/dashboard" to [routerLink]="['/dashboard']" to get the links to have an href attribute. 然后,我不得不将routerLink="/dashboard"更改为[routerLink]="['/dashboard']"以使链接具有href属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM