Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex layout not working on angular 5

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

like image 828
Kerby82 Avatar asked Jan 30 '18 23:01

Kerby82


People also ask

How do I add layout to angular Flex?

From your project folder, run the following command to install Flex Layout: npm install @angular/flex-layout @10.0. 0-beta.

Is angular Flex layout responsive?

lt-md. The output is as shown below. Using Angular Flex layouts, we can create any HTML layouts that are responsive on all devices.

What is Flex layout in angular?

Angular flex-layout is a stand-alone library developed by the Angular team for designing sophisticated layouts. Angular Layout provides a sophisticated API using Flexbox. The module provides Angular developers with component layout features using a custom layout API.

What is fxLayoutGap?

fxLayoutGap is used to specify gap between flex children items inside flex container. fxLayoutGap is an optional API. fxLayoutGap directive should be added to parent container i.e, flex container.


1 Answers

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

like image 90
Mehdi Benmoha Avatar answered Oct 04 '22 14:10

Mehdi Benmoha