Angular2 Material: Md-Menu opens below router outl

2019-05-18 17:16发布

问题:

When using md-menu with @angular/material, said menu opens below my router outlet no matter how i stack the components. What am i missing here?

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>

As stated above, when clicking on the menu, it opens below my block of <p> ipsum. What am i missing to have the menu open over the content?

I am also experiencing an issue with firefox wherein clicking outside the menu does not collapse the menu back. After some digging, I understand this is most likely a bug with Material. I don't believe they are related however I also don't believe I know what I'm talking about most of the time... This is why i included it here. Thanks SO!

回答1:

The issue was that I hadn't declared a global theme as described in the github docs for angular/material

This was accomplished by adding the following to my root styles:

styles.css

@import '~@angular/material/core/theming/prebuilt/deeppurple-amber.css';

Alternatively, you can add a link into your index.html.

<link href="node_modules/@angular/material/core/theming/prebuilt/deeppurple-amber.css" rel="stylesheet">

BTW: This also fixed my Firefox bug as well