【问题标题】:Reusable Sidenav in Angular2 Material DesignAngular2 材料设计中的可重用 Sidenav
【发布时间】:2017-05-31 10:53:41
【问题描述】:

我已经开始学习 Angular2 和 Material design。我想创建一个具有以下组件的应用程序。参考下图

  1. 汉堡菜单按钮。点击它会显示侧面板
  2. 侧面板。它将有其他几个选项。此菜单在所有应用屏幕上通用

我创建了 3 个在图像中可见的组件。 right-menu-component.html 如下所示

<md-sidenav #sidenav mode="side" class="app-sidenav">
  <ul>
    <li>Menu Item</li>
    <li>Menu Item</li>
    <li>Menu Item</li>
    <li>Menu Item</li>
    <li>Menu Item</li>
    <li>Menu Item</li>
  </ul>
</md-sidenav>

我想知道如何在点击按钮时显示此菜单,无论是从主屏幕还是关于屏幕

docs 中的示例在单个页面中显示了实现,但不是作为可重复使用的菜单组件

编辑 1: 我不想将按钮放在根页面上,因为登录、注册等某些屏幕没有菜单。但是如果将它们放在根页面上是一种更好的方法并且可以显示/隐藏按钮,那么我会添加它。

编辑 2: 我尝试使用 Angular 文档中的示例而不更改任何代码行。

<md-sidenav-container class="example-container">
  <md-sidenav #sidenav class="example-sidenav">
    Jolly good!
  </md-sidenav>

  <div class="example-sidenav-content">
    <button md-button (click)="sidenav.open()">
      Open sidenav
    </button>
  </div>

</md-sidenav-container>

当我点击“open sidenav”按钮时,控制台出现以下错误

EXCEPTION: Error in ./HomeComponent class HomeComponent - inline template:34:4 caused by: self._el_42.open is not a function

编辑 3: 以下是模块的版本

angular-cli: 1.0.0-beta.28.3
node: 6.9.5
os: darwin x64
@angular/animations: 4.1.3
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/material: 2.0.0-beta.3
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/compiler-cli: 2.4.10

【问题讨论】:

  • 显而易见的解决方案是将按钮和侧边栏放在根页面上并使用 更改其他所有内容
  • @elasticrash:请阅读编辑。另外,您能否分享一些从不同页面调用/显示正确菜单(sidenav)的代码
  • 还有其他更复杂的方法来实现这一点,例如将组件动态注入视图......例如通过路由保护。这与这种方法非常相似stackoverflow.com/questions/41602522/…

标签: angular angular2-forms


【解决方案1】:

创建一个服务来处理sidenav和工具栏之间的连接。

sidenav.service.ts

@Injectable()
export class SidenavService {
  toggle = new EventEmitter();

  constructor() {
  }
}

toolbar.component.ts

export class ToolbarComponent implements OnInit {

  constructor(public sidenavService: SidenavService) { }
}

toolbar.component.html

<md-toolbar color="primary" fxLayout="row" color="none">
  <button ... (click)="sidenavService.toggle.emit()"> ... </button>
</md-toolbar>

sidenav.component.ts

@ViewChild('sidenav') sidenav: MdSidenav;

constructor(private sidenavService: SidenavService) {

    this.sidenavService.toggle
       .asObservable()
       .subscribe(
          () => this.sidenav.toggle()
       );
}

sidenav.component.html

<md-sidenav #sidenav>...</md-sidenav>

【讨论】:

  • 我尝试了您描述的方式。单击该按钮会在 sidenav.component.ts 中显示错误“sidenav.toggle 不是函数”。 console.log(sidenav) 打印“ElementRef {nativeElement: md-sidenav.app-sidenav}”
  • 我再次对其进行了测试,但对我来说它正在工作。你能告诉我更多关于你的问题吗?或者您使用的是哪个版本的材料
  • 我在 Edit 3 中添加了版本
  • 如果你确定你已经像我一样做了所有事情,试着更新你的包。我正在研究 angular 4.1.3、cli 1.1.0 和 angular material beta-6,也许有些东西已经改变了 :-)
  • 将材料更新到 2.0,但在编译代码时出现很多错误。此外,文档 (material.angular.io/components/component/sidenav) 中的示例似乎在 Material 网站本身中无法正常工作。
猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 2017-09-23
  • 2017-03-02
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多