【问题标题】:How to setup Angular Flex Layout breakpoints with Angular 8如何使用 Angular 8 设置 Angular Flex 布局断点
【发布时间】:2020-02-17 03:19:22
【问题描述】:

有人知道如何使用 Angular 8 和 Angular Flex Layout 设置 Flex Layout 断点。我正在尝试在这样的模板中使用它们:

    <div fxFlex="100%" fxFlex.md="20%" fxFlex.gt-md="50%">
        My Content
    </div>

这目前不起作用。我的一些设置:

  • constants.ts 文件中的断点数组:
        import { BREAKPOINTS } from '@angular/flex-layout';

        export const OVERRIDE_BREAKPOINTS = [
            {
                alias: 'xs',
                mediaQuery: 'screen and (min-width: 1px) and (max-width: 599px)',
                overlapping: false
            },
            ...
        ]

         export const BreakPointsProvider = {
             provide: BREAKPOINTS,
             useValue: OVERRIDE_BREAKPOINTS,
             multi: true
         };
  • app.module.ts 中的设置:
        imports: [
            FlexLayoutModule.withConfig({}, OVERRIDE_BREAKPOINTS),
            ...
        ]


        providers: [
            BreakPointsProvider
            ...
        ]

我不知道我还应该添加什么才能使其正常工作。

提前致谢!

【问题讨论】:

    标签: angular breakpoints angular8 angular-flex-layout


    【解决方案1】:

    我最终使用了以前版本的 Angular 和最新版本的 Angular Flex Layout,这按预期工作:

    package.json:

    {
      "name": "angular-app",
      "version": "0.0.0",
      "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      },
      "private": true,
      "dependencies": {
        "@angular/animations": "^8.0.0",
        "@angular/cdk": "8.0.2",
        "@angular/common": "8.0.0",
        "@angular/compiler": "8.0.0",
        "@angular/core": "8.0.0",
        "@angular/flex-layout": "8.0.0-beta.26",
        "@angular/forms": "8.0.0",
        "@angular/material": "^8.0.2",
        "@angular/platform-browser-dynamic": "8.0.0",
        "@angular/platform-browser": "8.0.0",
        "@angular/router": "8.0.0",
        "@auth0/angular-jwt": "^3.0.0",
        "@types/jasmine": "2.8.16",
        "@types/jasminewd2": "2.0.8",
        "@types/node": "8.9.5",
        "core-js": "2.6.9",
        "hammerjs": "^2.0.8",
        "rxjs": "6.5.2",
        "zone.js": "0.9.1"
      },
      "devDependencies": {
        "@angular-devkit/build-angular": "~0.10.0",
        "@angular/cli": "~7.0.2",
        "@angular/compiler-cli": "~7.0.0",
        "@angular/language-service": "~7.0.0",
        "@types/jasmine": "~2.8.8",
        "@types/jasminewd2": "~2.0.3",
        "@types/node": "~8.9.4",
        "codelyzer": "~4.5.0",
        "jasmine-core": "~2.99.1",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "~3.0.0",
        "karma-chrome-launcher": "~2.2.0",
        "karma-coverage-istanbul-reporter": "~2.0.1",
        "karma-jasmine": "~1.1.2",
        "karma-jasmine-html-reporter": "^0.2.2",
        "karma-spec-reporter": "0.0.32",
        "protractor": "~5.4.0",
        "ts-node": "~7.0.0",
        "tslint": "~5.11.0",
        "typescript": "~3.1.1"
      }
    }
    

    app.module.ts:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
    import { ReactiveFormsModule, FormsModule } from '@angular/forms';
    
    import { FlexLayoutModule } from '@angular/flex-layout';
    import { AuthInterceptor } from './auth/auth-interceptor';
    
    @NgModule({
      declarations: [ ],
      imports: [
        AppMaterialModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        BrowserModule,
        FlexLayoutModule,
        FormsModule,
        HttpClientModule,
        ReactiveFormsModule,
        SnackBarModule
      ],
      providers: [
        { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
      ],
      bootstrap: [AppComponent],
      entryComponents: [ ]
    })
    export class AppModule { }
    

    我希望它对某人有所帮助。

    【讨论】:

    • man Angular 的版本升级太糟糕了。我正在使用最新的 Angular 版本,但找不到自定义断点的工作示例。恶心。
    【解决方案2】:

    你需要像这样更新你的 HTML:

    <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>
    

    for more information click here

    【讨论】:

    • 感谢您的评论,这就是我想要做的,使用断点,例如:xs、sm、md、lg、xl,但这些不适用于最新的 Angular 版本。跨度>
    猜你喜欢
    • 2019-10-14
    • 2020-09-16
    • 2019-07-24
    • 1970-01-01
    • 2019-07-14
    • 2018-06-17
    • 2018-06-20
    • 2018-04-28
    • 2018-03-06
    相关资源
    最近更新 更多