代码之家  ›  专栏  ›  技术社区  ›  Immad Hamid

(Angular ng toolkit)生成或运行服务器时没有错误,但浏览器没有显示任何内容

  •  0
  • Immad Hamid  · 技术社区  · 6 年前

    我用过 ng-toolkit 具有 ng add @ng-toolkit/universal 为我的项目添加角度通用支撑。

    组件:

    export class BannerComponent {
    
      slides: any[] = [
        // tslint:disable-next-line:max-line-length
        { image: 'assets/banner/banner-one.png' },
        // tslint:disable-next-line:max-line-length
        { image: 'assets/banner/banner-two.png' },
        // tslint:disable-next-line:max-line-length
        { image: 'assets/banner/banner-three.png' }
      ];
    
    }
    

    <section class="sec-space-b" id="banner">
        <mat-carousel
            timings="250ms ease-in"
            [autoplay]="true"
            interval="5000"
            color="accent"
            maxWidth="auto"
            proportion="25"
            slides="5"
            [loop]="true"
            [hideArrows]="false"
            [hideIndicators]="false"
            [useKeyboard]="true"
            [useMouseWheel]="false"
            orientation="ltr"
          >
            <mat-carousel-slide
                #matCarouselSlide
                *ngFor="let slide of slides; let i = index"
                overlayColor="#00000000"
                [image]="slide.image"
                [hideOverlay]="false"
            ></mat-carousel-slide>
        </mat-carousel>
    </section>
    

    我怎样才能解决这个问题?我能从服务器端构建中排除特定的组件吗?

    0 回复  |  直到 6 年前
        1
  •  6
  •   Maciej Treder    6 年前

    PLATFORM_ID 令牌和 isPlatformBrowser isPlatformServer

    在模板中使用 #ngIf 声明:

    <section class="sec-space-b" id="banner" *ngIf="isBrowser">
    

    在组件代码中初始化 isBrowser

    import { isPlatformBrowser } from '@angular/common';
    import { Component, OnInit, Inject, PLATFORM_ID } from '@angular/core';
    
    
    @Component({
      selector: 'app-home-banner',
      templateUrl: './banner.component.html',
      styleUrls: ['./banner.component.scss']
    })
    export class BannerComponent implements OnInit {
    
      public isBrowser = isPlatformBrowser(this.platformId);
    
      constructor(@Inject(PLATFORM_ID) private platformId: any) { }
    }
    

    你可以读更多关于 isPlatformServer服务器 在本文中(此处使用): https://www.twilio.com/blog/create-search-engine-friendly-internationalized-web-apps-angular-universal-ngx-translate

    你也可以看看我关于Angular Universal的演讲(13:26-关于在浏览器和服务器上运行不同的代码): https://www.youtube.com/watch?v=J42mqpVsg0k