首页 > 解决方案 > Angular Library : Material Menu : Error: Export of name 'matMenu' not found

问题描述

I'm working on an Angular Library, I have this simple menu :

<mat-menu #menu="matMenu" yPosition="below" style="margin-top: 45px;">
    <button style="outline: none;" *ngFor='let button of card?.overflowmenu?.buttons' mat-menu-item
            class="header-menu-item">
        {{button.text}}
    </button>
</mat-menu>

And the module looks like this.

import { NgModule } from '@angular/core';
import { CardComponent } from './card.component';
import { MatMenuModule } from '@angular/material/menu';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  declarations: [CardComponent],
  imports: [
    BrowserModule,
    CommonModule,
    BrowserAnimationsModule,
    MatMenuModule,
  ],
  exports: []
})
export class CardModule { }

When I do : $ng test @libraryname.

I get the error: Error: Export of name 'matMenu' not found!

Is there something I'm missing to import when building an Angular library?

标签: angularangular-material

解决方案


您的测试也需要导入模块 - 所以您需要

import { MatMenuModule } from '@angular/material/menu';

在您的 .spec.ts 文件的导入中,然后

    TestBed.configureTestingModule({
      declarations: [YourComponent],
      imports: [
        NoopAnimationsModule,
        LayoutModule,
...
        MatMenuModule,
      ]
    }).compileComponents();

在你的测试文件中


推荐阅读