gpt4 book ai didi

尽管在 dom 中,Angular Material2 sidenav 并未显示

转载 作者:行者123 更新时间:2023-12-02 11:25:05 26 4
gpt4 key购买 nike

我遇到一个问题,侧导航和 header 是共享服务的不同组件。在检查 sidenav 元素时,这似乎有效,我可以看到打开和关闭的切换。我遇到的问题是实际上没有出现 sidenav。

md-sidenav not appearing visually, although it is in the DOM

组件文件:

import { Component, OnInit, ViewChild } from '@angular/core';

import { MdSidenav } from '@angular/material';

import { SidenavToggleService } from './shared/sidenavToggle.service';

@Component({
selector: 'app-sidenav',
templateUrl: './sidenav.component.html',
styleUrls: ['./sidenav.component.css']
})
export class SidenavComponent implements OnInit {
@ViewChild('sidenav') sidenav: MdSidenav;

/**
* Constructor of the class.
* @param {sidenavService} SidenavToggleService
*/
constructor(public sidenavService: SidenavToggleService) {}

/**
* OnInit life cycle hook
*/
public ngOnInit(): void {
this.sidenavService.setSidenav(this.sidenav);
}

}

html 文件:

<md-sidenav-container>
<md-sidenav #sidenav mode="over">
testerer
</md-sidenav>
</md-sidenav-container>

服务文件:

import { Injectable } from '@angular/core';
import { MdSidenav, MdSidenavToggleResult } from '@angular/material';

@Injectable()
export class SidenavToggleService {
private sidenav: MdSidenav;

/**
* Setter for sidenav.
*
* @param {MdSidenav} sidenav
*/
public setSidenav(sidenav: MdSidenav) {
this.sidenav = sidenav;
}

/**
* Open this sidenav, and return a Promise that will resolve when it's fully opened (or get rejected if it didn't).
*
* @returns Promise<MdSidenavToggleResult>
*/
public open(): Promise<MdSidenavToggleResult> {
return this.sidenav.open();
}

/**
* Close this sidenav, and return a Promise that will resolve when it's fully closed (or get rejected if it didn't).
*
* @returns Promise<MdSidenavToggleResult>
*/
public close(): Promise<MdSidenavToggleResult> {
return this.sidenav.close();
}

/**
* Toggle this sidenav. This is equivalent to calling open() when it's already opened, or close() when it's closed.
*
* @param {boolean} isOpen Whether the sidenav should be open.
*
* @returns {Promise<MdSidenavToggleResult>}
*/
public toggle(isOpen?: boolean): Promise<MdSidenavToggleResult> {
console.log('uh');
return this.sidenav.toggle(isOpen);
}
}

编辑:添加了我正在使用的服务

最佳答案

请在sidenav中设置fixedInViewport,将解决该问题

<md-sidenav #sidenav mode="over" [fixedInViewport]="true" >
<!-- content -->
</md-sidenav>

关于尽管在 dom 中,Angular Material2 sidenav 并未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com