当使用带有@ angular / material的md-menu时,无论我如何堆叠组件,所述菜单都会在我的路由器插座下方打开。我在这里缺少什么?
app.modules.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Router } from '@angular/router';
import { MaterialModule } from '@angular/material';
import { MdButtonModule } from '@angular/material';
import { MdIconModule } from '@angular/material';
import { AppComponent } from './app.component';
import { ROUTES } from './app.routes';
import { ErrorComponent } from './error.component';
import { HomeComponent } from './home/home.component';
import { NavComponent } from './nav/nav.component';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [
AppComponent,
ErrorComponent,
HomeComponent,
NavComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
MdButtonModule,
MdIconModule,
RouterModule.forRoot(ROUTES),
MaterialModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.routes.ts
import {Routes} from '@angular/router';
import {AppComponent} from './app.component';
import {ErrorComponent} from './error.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
export const ROUTES: Routes = [
{path: '', component: HomeComponent },
{path: 'login', component: LoginComponent },
{path: '**', component: ErrorComponent}
];
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<app-nav></app-nav>
<router-outlet></router-outlet>
`
})
export class AppComponent {
}
nav.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css'] # No styles applied.
})
export class NavComponent {
}
nav.component.html
<md-toolbar>
<button md-icon-button [md-menu-trigger-for]="menu">
<md-icon class="dark"> menu </md-icon>
</button>
<md-menu #menu="mdMenu">
<button md-menu-item [routerLink]="['']">Home</button>
<button md-menu-item [routerLink]="['login']">Login</button>
<button md-menu-item> Help </button>
<button md-menu-item disabled> Sign Out </button>
</md-menu>
</md-toolbar>
home.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html'
})
export class HomeComponent {
}
home.component.html
<p> With a bunch of ipsum <p>
如上所述,点击菜单后,它会在<p>
ipsum块下面打开。在内容上打开菜单我错过了什么?
我也遇到了firefox的问题,其中点击菜单外部不会折叠菜单。经过一番挖掘,我明白这很可能是Material的一个bug。我不相信他们是相关的,但是我也不相信我知道我在谈论大部分时间...这就是为什么我把它包括在这里。谢谢!
答案 0 :(得分:3)
问题在于我没有声明github docs中描述的角度/材料的全局主题
这是通过在我的根样式中添加以下内容来完成的:
<强> styles.css的强>
@import '~@angular/material/core/theming/prebuilt/deeppurple-amber.css';
或者,您可以在index.html中添加链接。
<link href="node_modules/@angular/material/core/theming/prebuilt/deeppurple-amber.css" rel="stylesheet">
BTW:这也修复了我的Firefox错误