【发布时间】:2019-07-11 21:23:45
【问题描述】:
我正在使用 bootstrap 4 和 angular 7,我有以下元素,它的功能是隐藏/显示侧边栏。
<a
class="app-sidebar__toggle"
href="#"
data-toggle="sidebar"
aria-label="Hide Sidebar"
></a>
问题是当我进入特定路线时,这会重新加载所有页面。这些是我在 app-routing.module.ts 中的路由
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'catalogo/lista', component: CatalogoListaComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];
所以,如果我输入http://localhost:4200/home,就会发生错误,但如果我输入任何其他路由(我的默认空路由将重定向到/home),例如http://localhost:4200 或http://localhost:4200/a_route_that_not_exists,我被重定向到 /home(如预期的那样),显示/隐藏侧边栏的按钮效果很好。
我希望我的问题很清楚,我希望你能帮我很多。
编辑:我的应用程序的更多代码...
这是我的 app.component.html:
<app-header></app-header>
<app-sidebar></app-sidebar>
<div class="app-content">
<app-title [titulo]="titulo" [icono]="icono [breadcrumbs]="breadcrumbs"></app-title>
<router-outlet></router-outlet>
</div>
这是我的 header.component.html(我使用链接显示/隐藏侧边栏):
<header class="app-header">
<a class="app-header__logo" href="index.html">Vali</a>
<!-- Sidebar toggle button-->
<a
class="app-sidebar__toggle"
href="#"
data-toggle="sidebar"
aria-label="Hide Sidebar"
></a>
<p>.... more html</p>
</header>
这是我的 sidebar.component.html:
<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
<aside class="app-sidebar">
<div class="app-sidebar__user">
<img
class="app-sidebar__user-avatar"
src="https://s3.amazonaws.com/uifaces/faces/twitter/jsa/48.jpg"
alt="User Image"
/>
<div>
<p class="app-sidebar__user-name">John Doe</p>
<p class="app-sidebar__user-designation">Frontend Developer</p>
</div>
</div>
<ul class="app-menu">
<li>
<a class="app-menu__item" [routerLink]="['/home']">
<i class="app-menu__icon fa fa-home"></i>
<span class="app-menu__label">Inicio</span>
</a>
</li>
more menu elements...
<ul>
</aside>
【问题讨论】:
-
您如何导航到
catalogo/lista路线?您是否在页面中使用 [routerLink] 指令?另外(只是为了仔细检查),您的侧边栏是否在您的<router-outlet>元素之外? -
是的,我正在使用 [routerLink] 来访问我的路由,并且侧边栏在
之外,只有当我在浏览器中输入完整的 url 时出现问题,例如 @987654324 @ 或 localhost:4200/home。如果我进入路由localhost:4200,或任何其他不存在的路由,我将被重定向到家并且应用程序运行良好 -
编辑:刚刚看到你的编辑,我明白现在发生了什么更好!输入直接 URL 时页面是否无法正常加载?还是其他组件加载不正确?
-
页面和组件加载良好,但是当我单击显示/隐藏侧边栏的按钮(我的问题开头的 元素)时,页面被重新加载(尝试去到 # in href),而不是显示/隐藏侧边栏
-
由于
href="#"的存在,点击a元素将始终触发路由操作。删除 href 是否会破坏引导操作?如果是这样,您几乎可以肯定在 Angular 中重新创建它。如果您提供一些通用组件代码(显示包含侧边栏和a元素的组件),我可以向您展示如何完成此操作!
标签: css angular web bootstrap-4