如何在角度5中的ngOnInit上调用NgbCarousel的pause()函数

Fer*_*qui 4 angular-cli ng-bootstrap angular

我是 Angular 的新手,并在我的用户界面中使用 ngbootstrap 。默认情况下,我无法在暂停模式下加载 NgbCarousel。下面是我尝试过的代码

    import { Component, OnInit, ViewChild } from '@angular/core';
    import { NgbCarousel } from '@ng-bootstrap/ng-bootstrap';
    @Component({
      selector: 'app-dashboard',
      templateUrl: './dashboard.component.html',
      styleUrls: ['./dashboard.component.css'],
      providers: [NgbCarousel] // add NgbCarouselConfig to the component providers

    })
     export class DashboardComponent implements OnInit {

    constructor(private auth: AuthService, private ngbCarousel: NgbCarousel) {}
    ngOnInit() {
        this.ngbCarousel.pause();
    }
    }
Run Code Online (Sandbox Code Playgroud)

下面是html文件:

<div class="jumbotron">
  <div class="container">
    <div class="row">
      <div class="col-12 col-lg-9">
        <ngb-carousel>
          <ng-template ngbSlide>
            <img src="https://lorempixel.com/900/500?r=1" alt="Random first slide">
            <div class="carousel-caption">
              <h3>First slide label</h3>
              <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
            </div>
          </ng-template>
          <ng-template ngbSlide>
            <img src="https://lorempixel.com/900/500?r=2" alt="Random second slide">
            <div class="carousel-caption">
              <h3>Second slide label</h3>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
          </ng-template>
          <ng-template ngbSlide>
            <img src="https://lorempixel.com/900/500?r=3" alt="Random third slide">
            <div class="carousel-caption">
              <h3>Third slide label</h3>
              <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
            </div>
          </ng-template>
        </ngb-carousel>
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

但暂停不起作用,我没有收到任何错误。这是调用暂停方法的正确方法吗?请指导我。

Edr*_*ric 5

编辑:请使用生命周期钩子,因为在组件的视图初始化AfterViewInit调用此钩子(有关更多信息,请参阅 Angular 的生命周期钩子文档):

import { AfterViewInit, ViewChild } from '@angular/core';
// ...
export class DashboardComponent implements AfterViewInit {
  @ViewChild('carousel') carousel: NgbCarousel;
  ngAfterViewInit() {
    this.carousel.pause();
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 删除组件NgbCarousel的提供者,因为根据文档NgbCarousel它是一个组件不是服务:

    @Component({
      selector: 'app-dashboard',
      templateUrl: './dashboard.component.html',
      styleUrls: ['./dashboard.component.css']
      // Remove providers array
    })
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在元素上添加模板引用<ngb-carousel>并使用ViewChild带有模板引用的 a 作为选择器,然后调用pause实例ViewChild

    <ngb-carousel #carousel>
      <!-- ... -->
    </ngb-carousel>
    
    Run Code Online (Sandbox Code Playgroud)
    import { AfterViewInit, /* OnInit */, ViewChild } from '@angular/core';
    // ...
    export class DashboardComponent implements AfterViewInit {
      @ViewChild('carousel') carousel: NgbCarousel;
      ngAfterViewInit() {
        this.carousel.pause();
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • @Edric`错误类型错误:无法读取未定义的属性'暂停'id设置为`&lt;ngb-carousel #carousel&gt;` (2认同)
  • 就我而言,问题是我使用了 `@ViewChild('carousel', {static: true}) carousel: NgbCarousel;`。它可以在没有“{static: true}”的情况下工作 (2认同)