12

I'm trying to use flex-layout on angular 5 but it's not working.

This is my environment:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.6.1
Node: 9.3.0
OS: darwin x64
Angular: 5.1.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 5.0.3
@angular/cli: 1.6.1
@angular/flex-layout: 2.0.0-beta.12-67e4bf5
@angular/material: 5.0.3
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.1
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.4.2
webpack: 3.10.0

This is the import in app.module.ts:

import {FlexLayoutModule} from "@angular/flex-layout";


@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    MaterialModule,
    SocialLoginModule,
    FlexLayoutModule
  ],

No error on compilation.

This is a test on a template:

<div class="container"
     fxLayout
     fxLayout.xs="column"
     fxLayoutAlign="center"
     fxLayoutGap="10px"
     fxLayoutGap.xs="0">
  <div class="item item-1" fxFlex="15%">Item 1</div>
  <div class="item item-2" fxFlex="20%" fxFlexOrder="3">Item 2</div>
  <div class="item item-3" fxFlex>Item 3</div>
</div>

<div class="container"
     fxLayout
     fxLayout.xs="column"
     fxLayoutAlign="center"
     fxLayoutGap="10px"
     fxLayoutGap.xs="0">
  <div class="item item-4" fxFlex fxFlexOffset="50px"  fxFlexOffset.xs="0">Item 4</div>
  <div class="item item-5" fxFlex="200px">Item 5</div>
</div>

This is the result (not what expected):

enter image description here

5
  • Why do you need a flex layout module? Does the CSS flexbox layout not work? Commented Jan 31, 2018 at 0:22
  • @Pytth the @angular/flex-layout module is implementing CSS Flexbox using Angular directives. Commented Jan 31, 2018 at 1:34
  • Are there any errors in the console? Commented Jan 31, 2018 at 1:35
  • 1
    Please add a small reproduction of the issue in stackblitz or similar. Commented Jan 31, 2018 at 7:56
  • Same here with same Flex version and Angular 5.0.4 Commented Feb 6, 2018 at 14:50

3 Answers 3

31

You have to import FlexLayoutModule in every feature module. This is not the best solution, but a working one.

Importing and EXPORTING FlexLayoutModule in the SharedModule works like charm and is the best solution that respects the angular style guide, by the way the SharedModule is imported in every feature module.

For more information about the SharedModule: https://angular.io/guide/styleguide#shared-feature-module

Edit: I also wrote an article about SharedModule vs CoreModule if you can't see the difference between them: https://medium.com/@benmohamehdi/angular-best-practices-coremodule-vs-sharedmodule-25f6721aa2ef

Sign up to request clarification or add additional context in comments.

4 Comments

Yes! Why is it not so obvious in the documentation that you have to import it into every feature module? Works perfectly in SharedModule.
Thank you! This is a case of a false negative or something. Absolutely no indication on screen/dev tools that flex-layout was missing! For anyone who has developed modules for secure areas, for instance, you'll need to add this module to it. The App.module is la donna mobile. I wish everything in the project would inherit from it. but, it don't work that way....
Remember you need to export it from your SharedModule too ;)
I just edited the answer to show in clear that you need both. Thanks for your feedback
3

Add the following to the package.json file:

"@angular/flex-layout": "^6.0.0-beta.15",
"rxjs": "^6.1.0",
"@angular/cdk": "^6.0.0",
"rxjs-compat": "6.0.0-beta.1",

and run

npm install

Comments

-2

Install Flex Layout from npm: $ npm install @angular/flex-layout --save

Or with Yarn: $ yarn add @angular/flex-layout

Now import FlexLayoutModule in your app module

import { FlexLayoutModule } from "@angular/flex-layout";
@NgModule({
  imports: [
    BrowserModule,
    FlexLayoutModule
  ],
  // ...
})
export class AppModule {}

Flex Layout is now ready to be used in your templates.

1 Comment

TS2339: Property 'forRoot' does not exist on type 'typeof FlexLayoutModule'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.