我正在使用 ngx-admin 并且有一个关于如何动态减小侧边栏大小的问题。此外,当我最小化浏览器时,侧边栏应根据浏览器大小自动调整大小。(它应该是响应式的)。
问问题
3859 次
2 回答
1
你可以在 github 上找到我对 ngx-admin 演示代码的回答的实时示例。因此,您可以使用 nebular 中包含的 layoutService 来使侧边栏具有响应性。示例:
<nb-sidebar class="menu-sidebar" tag="menu-sidebar" responsive>
<nb-menu [items]="YourItems"></nb-menu>
</nb-sidebar>
然后制作一个按钮将其切换到展开、压缩和折叠之间。这是用于切换和自动处理响应性的按钮及其代码。
<a (click)="toggleSidebar()" class="sidebar-toggle">
<nb-icon icon="menu-2-outline"></nb-icon>
</a>
然后在您的 ts 文件中使用布局服务:
import { NbSidebarService } from '@nebular/theme';
import { LayoutService } from '../../@core/utils';
constructor(private layoutService: LayoutService, private sidebarService:NbSidebarService){}
toggleSidebar() {
this.sidebarService.toggle(true, 'menu-sidebar');
this.layoutService.changeLayoutSize();
return false;
}
最后决定侧边栏宽度只需将它们与变量一起注册到默认主题示例:
@import "~@nebular/theme/styles/theming";
@import "~@nebular/theme/styles/themes/default";
$nb-themes: nb-register-theme(
(
sidebar-width: 16rem,
sidebar-width-compact: 3.5rem
),
default,
default
);
于 2020-04-06T09:02:41.410 回答
1
我找到了自己的答案。我将文件 one-column.layout.scss 更改为此。
@import '../../styles/themes';
@import '~bootstrap/scss/mixins/breakpoints';
@import '~@nebular/theme/styles/global/breakpoints';
@include nb-install-component() {
.menu-sidebar ::ng-deep .scrollable {
padding-top: nb-theme(layout-padding-top);
}
}
@include nb-install-component() {
.menu-sidebar ::ng-deep .main-container.main-container-fixed {
width: 10rem;
}
}
@include nb-install-component() {
.expanded {
width: 10rem;
}
}
并将文件 one-column.layout.ts 更改为此
import { AfterViewInit, Component, Inject, PLATFORM_ID, ViewChild } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { NbLayoutComponent } from '@nebular/theme';
import { WindowModeBlockScrollService } from '../../services/window-mode-block-scroll.service';
@Component({
selector: 'ngx-one-column-layout',
styleUrls: ['./one-column.layout.scss'],
template: `
<nb-layout windowMode>
<nb-layout-header fixed>
<ngx-header></ngx-header>
</nb-layout-header>
<nb-sidebar state="expanded" class="menu-sidebar" style="width: 'auto'" tag="menu-sidebar" responsive>
<ng-content select="nb-menu"></ng-content>
</nb-sidebar>
<nb-layout-column>
<ng-content select="router-outlet"></ng-content>
</nb-layout-column>
<nb-layout-footer fixed>
<ngx-footer></ngx-footer>
</nb-layout-footer>
</nb-layout> ` })
export class OneColumnLayoutComponent implements AfterViewInit {
@ViewChild(NbLayoutComponent, { static: false }) layout: NbLayoutComponent;
constructor(
@Inject(PLATFORM_ID) private platformId,
private windowModeBlockScrollService: WindowModeBlockScrollService) {}
ngAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
this.windowModeBlockScrollService.register(this.layout);
}
}
}
于 2020-04-06T10:36:14.330 回答