首页 > 解决方案 > 无法在我的功能模块中导入 Angular flex-layout

问题描述

我正在尝试在我的一个模块中使用 Angular flex-layout,但我无法使其工作。当我在应用程序模块中导入 flex-layout 时,它工作得很好,但只在应用程序组件中。当我在另一个模块中导入 flex-layout 时,它根本不起作用。我没有收到任何错误,它只是不起作用。

我的代码:

// App module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FlexLayoutModule } from '@angular/flex-layout';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FlexLayoutModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

.

// AppComponent HTML - WORKS
<div fxLayout="row" fxLayoutAlign="center center">
  <div fxFlex="30%">
    example
  </div>
</div>

.

// Other module
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FlexLayoutModule } from '@angular/flex-layout';

import { SomeRoutingModule } from './some-routing.module';
import { SomeComponent } from './pages/some-component/some-component.component';


@NgModule({
  declarations: [SomeComponent],
  imports: [
    CommonModule,
    FlexLayoutModule,
    SomeRoutingModule
  ]
})
export class GameModule { }

.

// SomeComponent HTML - DOESN'T WORK
<div fxLayout="row" fxLayoutAlign="center center">
  <div fxFlex="30%">
    example
  </div>
</div>

标签: javascriptangularangular-materialangular-flex-layout

解决方案


尝试在 Imports 下的 AppModule 中导入 GameModule。

但是,我建议你创建一个共享模块并将 flex-layout 放在那里,因为最新的 Angular 版本支持共享模块并且被认为是最佳实践。有关如何创建共享模块的更多说明如下: https ://angular.io/guide/styleguide#shared-feature-module


推荐阅读